undefined (Constant/static)

A literal constant whose type is a built-in primitive value.

Availability:

ECMAScript edition - 2
JavaScript - 1.3
JScript - 5.5
Internet Explorer - 5.5
Netscape version - 4.06
Property/method value type:Undefined primitive

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.

Warnings:

Example code:

   // 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

Cross-references:

ECMA 262 edition 2 - section - 4.3.9

ECMA 262 edition 3 - section - 4.3.9

O'Reilly JavaScript Definitive Guide - page - 47