The undefined value is a primitive value that is returned when a variable has not yet been assigned a value.
In some implementations this value compares equal to the null value. They are not actually the same, but this can sometimes provide a work-around for those implementations that do not provide a way to explicitly test for an undefined value.
If a variable is declared and no value is assigned to it, then it will contain the undefined value. It will exist and can be referred to in expressions and will affect them to the extent they can be affected by the value 'undefined'.
If a variable has never been declared and it is referred to, a run-time error will result. It does not exist let alone contain the 'undefined' value. There is one instance where referring to a non-existent variable does not generate an error, and that is to use it as an LValue in an assignment. Only the = operator can be used. The prefix and postfix increment/decrement operators should yield an error and so should the compound assignment operators, since the value to the left does not exist until after the assignment has taken place.
You cannot create an 'undefined' value explicitly in older interpreters since there is no keyword to yield it as a constant. This is more of a problem with MSIE which only supported the undefined keyword as of version 5.5 while Netscape has supported it since JavaScript 1.3. If you need to, you can manufacture one of your own with this expression:
undefined = (void 0);
This whole area is somewhat mysterious and the (void 0) simulation really creates a null value which some browsers cannot distinguish from the undefined value.
Some implementations do not provide adequate protection against you corrupting this value. Be careful not to assign your own values to this variable. It will lead to unpredictable results if you do.
This is not available for use server-side with Netscape Enterprise Server 3.
// Assuming a or b has not been declared, this generates an error a += 10; // This doesn't a = 10; // This does alert(b);
See also: | Exception, Global object, null, Null literal, Range error, void |
ECMA 262 edition 2 - section - 4.3.9
ECMA 262 edition 3 - section - 4.3.9
O'Reilly JavaScript Definitive Guide - page - 47
Prev | Home | Next |
Unary operator | Up | Undefined behaviour |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |