SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Posts
    87
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Check if variable is set

    Hello - silly question I'm sure, but is there a JavaScript equivalent to the PHP isset() function? Basically, I just need to see if an array has already been created. If not, I'll create it. If so, I'll add to it. I tried something like...
    Code:
    if (!myArray) {
    var myArray = new Array();
    myArray[0] = someString;
    n = 1;
    } else {
    myArray[n] = someString;
    n++;
    }
    ...but just got an error message that myArray was undefined. What's the "right" way to do this? Thanks in advance.
    Nate Baldwin
    mindpalette.com

  2. #2
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    use the typeof operator.

    if ( typeof myArray == 'undefined' ) {
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  3. #3
    SitePoint Addict
    Join Date
    May 2000
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is a link to a short tutorial on the subject:
    http://www.javascriptkit.com/javatut...rminevar.shtml

  4. #4
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Posts
    87
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sounds about like what I was needing - thank you both :-)
    Nate Baldwin
    mindpalette.com

  5. #5
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks from me also; I've been setting all my variable to false at the start.
    Code:
    myArray=false;
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •