Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>An undeclared variable (that is, one that doesn't exist) does not have a type - and so its type <em>is</em> undefined. I believe that the generally accepted way to test if something is undefined is </p> <pre><code>typeof var === 'undefined' </code></pre> <p>rather than</p> <pre><code>var === undefined </code></pre> <p>since if the variable does not exist, you cannot access it. The latter case might be more useful if you are sure the variable <em>should</em> exist, since the difference between the two is that an undeclared variable will return false in the first case but cause an error in the second case.</p> <p>Make sure that you use the triple-equals operator though if you're using the second variant; the (more usual) double-equals operator performs type coercion, so <code>null == undefined</code> is true while <code>null === undefined</code> is false. Sometimes you might want the first behaviour, though often you'll want the second, especially if you're testing against undefined, so it's important to recognise the difference. (This is another benefit of the first case since it's not possible to make this subtle error).</p> <p>Yes, variables can have a value of undefined and you can explicitly assign values to them. Assigning <code>undefined</code> to a variable though is probably confusing, since it's a bit of a paradox (you've defined the variable as undefined) and it's not possible to distinguish that variable from either variables that don't exist or uninitialised variables. Use null if you want to signify that a variable has no value currently, or if you want to "undeclare" the variable altogether, use <code>delete {varname}</code>. Assigning undefined to a variable does not remove that variable's declaration; and it will still appear in the list of its owning object's properties if you iterate over them, so I can't think of any situation this would benefit you.</p> <p>Edit: In response to the comment, when comparing the values with === (or ==), the variable must be deferenced to obtain its current value in order to do the comparison. Since the variable doesn't exist, this dereferencing fails (no real surprises so far). While you might expect the typeof operator to work in a similar way (get the current value, see what its type is) it specifically works with undefined variables. The "short version" (as used by the <a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Operators/Special_Operators/typeof_Operator" rel="noreferrer">Mozilla reference</a> ) is simply that "The typeof operator returns a string indicating the type of the unevaluated operand."</p> <p>The long version, extracted from the <a href="http://www.mozilla.org/js/language/E262-3.pdf" rel="noreferrer">ECMAScript spec</a>, is that there's a special case for undefined variables:</p> <blockquote> <p>11.4.3 typeof <em>UnaryExpression</em> is evaluated as follows:</p> <ol> <li>Evaluate <em>UnaryExpression</em> (as per 10.1.4 this returns "a value of type Reference whose base object is <em>null</em> and whose property name is the identifier" if the variable is undeclared).</li> <li>If Type(Result(1)) is not Reference, go to step 4. (It is a Reference)</li> <li>If GetBase(Result(1)) is <em>null</em>, return "undefined". (This is the special case matching undefined variables)</li> </ol> </blockquote> <p>As for your comment to the first question, "how can you classify what does not exist" - it's easy! Simply divide all concepts into things that do exist (e.g. squirrel, rock) and those that don't exist (unicorns, warp drives). That's what undefined means; regardless of its semantics in English, its Javascript meaning is that the variable hasn't been declared or populated yet, and so while the <em>concept</em> of "a variable called foo" is valid, there exists no variable with that name holding a real value.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload