This page is Ready to Use

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

undefined

Summary

A value that has never been defined, such as a variable that has not been initialized.

Syntax

undefined

Examples

Usage of undefined with strict equality.

var foo;

if (foo === undefined) {
    // executes
} else {
   // won't execute
}

Usage of undefined with the typeof operator.

var foo;

if (typeof foo === 'undefined') {
    // executes
} else {
   // won't execute
}

Comparison of strict equality and the typeof operator. typeof doesn’t throw an error if the variable hasn’t been defined, because its value is implicitly undefined.

// foo variable is not defined
if (typeof foo === "undefined") {
    // executes
}

if (foo === undefined) {
    // throws ReferenceError
}

Remarks

The undefined constant is a member of the Global object, and becomes available when the scripting engine is initialized. When a variable has been declared but not initialized, its value is undefined.

If a variable has not been declared, you cannot compare it to undefined , but you can compare the type of the variable to the string "undefined". (see second example)

The undefined constant is useful when explicitly testing or setting a variable to undefined.

Usage

 undefined is not a reserved word, thus can be used as a variable in any scope except for the global scope.

Notes

undefined vs. null

undefined means a value is declared but not initialized, it doesn’t have a value assigned to it.

null can be assigned to a variable to represent no value.

Other than that, undefined is a type, while null is an object

See also

Other articles

Attributions

  • Microsoft Developer Network: Article