www.webdeveloper.com
Results 1 to 4 of 4

Thread: Undefined Vs. Undeclared Variables

  1. #1
    Join Date
    Jul 2010
    Location
    Cobalt, Ontario, Canada
    Posts
    15

    Undefined Vs. Undeclared Variables

    I would like to distinguish between an undeclared variable and a declared but undefined variable in JavaScript.

    Definition of UNDECLARED: There was no preceding var or global statement, and no statement that explicitly or implicitly assigns a value to the variable.

    Definition of DECLARED BUT UNDEFINED: There was a preceding var statement, but no value was assigned to the variable in the var statement or subsequently.

    The best I've found so far is:

    Code:
              typeof(obj)=="undefined"
    This test distinguishes between defined and undefined variables, but does not distinguish between "undefined" and "defined but undeclared." Can you help?

  2. #2
    Join Date
    Sep 2010
    Posts
    62
    Because javascript implicitly declares variables, it is impossible to tell if a variable exists through implicit or explicit declaration.

    If you expect a variable to be local and it is global you can assume it was implicitly declared because implicit variables are automatically given global scope regardless of where the declaration occurs.

    Other than the local/global check, I can't think of any way of determining if the variable was implicit/explicit.

    Could you tell us a little more about what you are trying to accomplish? There may be another way to do it.
    Quint Jensen
    Web Developer
    Scaled Solutions

  3. #3
    Join Date
    Jul 2010
    Location
    Cobalt, Ontario, Canada
    Posts
    15

    Just Wondering

    Quote Originally Posted by qjensen View Post
    ... Could you tell us a little more about what you are trying to accomplish? There may be another way to do it.
    I am not trying to accomplish anything other than a deeper understanding of JavaScript. I was working on a problem, but solved it by using typeof(obj)=="undefined". That's when I noticed that no distinction seems to be made between undeclared and undefined.

    Here is a short JavaScript I wrote to help me understand the nuances:

    Code:
    <script type="text/JavaScript">
    
    function showRow(state, x) {
      document.write ("<tr>");
      document.write ("<td>");
      document.write (state);
      document.write ("<td>");
      document.write (x);
      document.write ("<td>");
      document.write (x==undefined);
      document.write ("<td>");
      if (x==undefined) { document.write ("RTE"); } else { document.write (x.length) }
      document.write ("<td>");
      document.write (typeof(x));
      document.write ("<td>");
      document.write ((x)?"T":"F");
      document.write ("<td>");
      document.write ((x==null));
      document.write ("<td>");
      document.write ("</tr>");
    }
    
    document.write ("<html><head></head><body>");
    document.write ("<table><tr><th>State<th>write(obj)<th>==undefined<th>obj.length<th>typeof(obj)<th>if(obj)<th>==null<th></tr>");
    showRow ("does not exist", x);
    var x;
    showRow ("exists, but not assigned a value", x);
    x = null;
    showRow ("assigned null", x);
    x = "";
    showRow ("assigned \"\"", x);
    x = "a value";
    showRow ("assigned a non-empty string", x);
    document.write ("<tr><td><td><td><td><td><td></tr>");
    document.write ("</table>");
    document.write ("</body></html>");
    </script>
    I am beginning to think that JavaScript makes no distinction between an undeclared variable and a declared variable that has not been initialized. Does this sound true to you?

  4. #4
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by WarrenGaebel View Post
    I am beginning to think that JavaScript makes no distinction between an undeclared variable and a declared variable that has not been initialized. Does this sound true to you?
    no, that's not true.

    for globals, you can split hairs using simple window.hasOwnProperty("x") syntax.

    function are more complicated.

    consider a simple function:
    Code:
    function f(a){
     var b, c;
     return a==b;
    }
    it sounds like you might be tempted to think that b and c are redundant, but they are not.
    by declaring them locally, they block out the named-identifiers "b" and "c" from globals and/or wrapping function contexts.

    while c isnt't used in the two-line function above, the principle of reserving lexical scope entities to the most local level still applies.

    in short, it's all about privacy.
    you are correct that undefined===undefined
    Last edited by rnd me; 10-19-2010 at 02:38 AM.

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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