www.webdeveloper.com
Results 1 to 7 of 7

Thread: Detecting assigned and undefined variables

  1. #1
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,440

    Question Detecting assigned and undefined variables

    What is the best way to detect if a variable (global or local) has been created?

    In the following code, if you run it with the variable 'casefile' defined, the alert works as expected.
    When you run it with no casefile = assignment, neither alert is displayed.

    Why do I get no alert notification when 'casefile' is not assigned or defined?
    When a global or local varialbe has been accidently left unassigned or undefined,
    how should it be checked if maked a default assignment?

    Hope I'm making myself clear. When is a variable undefined or null and how is that condition checked so a default value could be created?


    Code:
    <!DOCTYPE HTML>
    <html>
    <head>
    <title> Untitled </title>
    <meta charset="utf-8">
    
    </head>
    <body>
    <script type="text/javascript">
    
    var str = '';
    
    // var casefile;                            // works fine if this variable is created
    // var casefile = 'The variable casefile';  // run script with this line IN and then commented OUT
    // NOTHING displays if both lines above are commented OUT
    
    if (casefile) { str += casefile+' is available\n\n'; }
             else { str += casefile+' is not available\n\n'; }
    
    if (casefile == undefined) { str += casefile+' is undefined\n\n'; }
                          else { str += casefile+' is not undefined\n\n'; }
    
    if (casefile == null) { str += casefile+' is null\n\n'; }
                     else { str += casefile+' is not null\n\n'; }
    
    alert(str);
    </script>
    
    </body>
    </html>

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,507
    Code:
    if(typeof varName == "undefined") alert("Undefined!");
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  3. #3
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    Hm... this is interesting but... you have to know some theory


    First when a variable is defined in top of the code, throughout the program (not locally) it becomes a property of a global object. In client-side JavaScript it becomes property of a window object.

    Now let's start with the first example when the casefile is defined but not initialized to any value:

    Code:
    var casefile;
    The first if statement:

    Code:
    if (casefile) { str += casefile+' is available\n\n'; }
             else { str += casefile+' is not available\n\n'; }
    would evaluate to false because casefile variable is defined and became a property of the global object but its value has not been initialized and so it is undefined. JavaScript type conversion specifies that any undefined value is converted to Boolean false and that's the reason why yo have result of "undefined is not available.


    The second if statement:

    Code:
    if (casefile == undefined) { str += casefile+' is undefined\n\n'; }
                          else { str += casefile+' is not undefined\n\n'; }
    evaluates to true because undefined equals undefined.

    The third if statement:

    Code:
    if (casefile == null) { str += casefile+' is null\n\n'; }
                     else { str += casefile+' is not null\n\n'; }
    i think that it gets evaluated to true, because JavaScript considers undefined and null as both values that represents absence of value but undefined represents some deeper kind of absence.

    You could check this with the strict equality operator:

    Code:
    if (casefile === null) { str += casefile+' is null\n\n'; }
                     else { str += casefile+' is not null\n\n'; }
    Now the result of the above conditional check is false.

    Ok, so what happens when you comment out both lines?

    If you check your debugger you'll see that there's "Reference error" thrown up. This is because in ECMAscript 5 strict mode if you don't define a variable you won't get away with it (its value won't be initialized to undefined) so your code fails at the first if statement because casefile is not defined.
    Last edited by tech_soul8; 11-05-2013 at 12:18 PM.

  4. #4
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    Quote Originally Posted by \\.\ View Post
    Code:
    if(typeof varName == "undefined") alert("Undefined!");
    As far as I know this ain't gonna happen in ECMAscript 5 strict mode if variable is not first defined. If a variable is defined but not initialized then its value becomes undefined. Otherwise "Reference error" is thrown up.

  5. #5
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    Quote Originally Posted by JMRKER View Post
    When is a variable undefined or null and how is that condition checked so a default value could be created?
    Undefined and null are both values that represent absence of a value as already stated in the previous reply.

    Undefined value specifies some deeper kind of absence. When you try to query a property of an object that doesn't exist the result would be an undefined value. The same is true when you try to access array element which doesn't exist. Undefined value is also returned as a result of function which doesn't return a value and as a value to an uninitialized variable or a value of a function parameter for which no argument has been supplied.

    Null typically means no value for string, number or an object.

  6. #6
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,440
    Thank you for your time and expertise.

  7. #7
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    You're welcome. I'm glad if I was able to help you.

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