www.webdeveloper.com
Results 1 to 3 of 3

Thread: problem setting variable

  1. #1
    Join Date
    Nov 2008
    Posts
    1

    problem setting variable

    Hello, I have the following code:

    var permissionsArray;

    function nonMSPopulatePP() {

    . . .

    permissionsArray = new Array("A", "B", "C");
    alert(permissionsArray); //displays "A,B,C"

    }

    when i try to access permissionsArray later on, after nonMSPopulatePP() has executed, it is undefined. The variable isn't declared twice.

    What could be some reasons for this?

    Thanks
    Eric

  2. #2
    Join Date
    Nov 2008
    Posts
    2
    Based on the most common, and most infuriating, similar problems I've had with JS, I'd recommend you check the following:

    • the 's' at the end of 'permissions' is actually there.
    • the 'A' in 'Array' is capitalized
    • The part of the function where you're declaring permissionsArray is actually being called (if in a conditional block)
    • You're not testing permissionsArray anywhere with a single '=' instead of a double '==' anywhere.
    • You're actually calling the function (and it has completed) before trying to access permissionsArray - I've had timing problems like this a lot using setTimeout, setInterval and various Ajax-related scripts

  3. #3
    Join Date
    May 2006
    Location
    Odenton, MD
    Posts
    1,449
    also, if you use var statement inside a function's body, the created variable become local to that function. in the snippet code isn't present, but in any case. so that if you do:
    Code:
    var permissionsArray;
    
    function nonMSPopulatePP() {
    . . .
    var permissionsArray = new Array("A", "B", "C");
    alert(permissionsArray); //displays "A,B,C"
    }
    permissionsArray set on the global environment still undefined.
    Last edited by ZeroKilled; 11-17-2008 at 10:25 AM.
    my mom is javascript, dad is javascripter, granpa is javascriptor, and my little sister is javasRidiculous.
    my nature language is javascript, then come spanish and english -- me

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles