Visit Mozilla.org

Core JavaScript 1.5 Reference:Global Properties:undefined

From MDC


[edit] Summary

The value undefined.

Core Global Property
Implemented in: JavaScript 1.3
ECMA Version: ECMA-262

[edit] Syntax

undefined

[edit] Description

undefined is a property of the global object, i.e. it is a variable in global scope.

The initial value of undefined is the primitive value undefined.

A variable that has not been assigned a value is of type undefined. A method or statement also returns undefined if the variable that is being evaluated does not have an assigned value. A function returns undefined if a value was not returned.

You can use undefined and the strict equality and inequality operators to determine whether a variable has a value. In the following code, the variable x is not defined, and the if statement evaluates to true.

var x;
if (x === undefined) {
   // these statements execute
}
if (x !== undefined) {
   // these statements do not execute
}

Note: The strict equality operator rather than the standard equality operator must be used here, because x == undefined also checks whether x is null, while strict equality doesn't. null is not equivalent to undefined. See comparison operators for details.

Alternatively, typeof can be used:

var x;
if (typeof x == 'undefined') {
   // these statements execute
}