Export (0) Print
Expand All

undefined Constant (JavaScript)

Returns an initial value of undefined.

undefined

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"

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

The following example illustrates the use of the undefined constant.

var newline = "<br />";
var s = "";

var declared;
if (declared == undefined)
    s += "declared has not been given a value";
else
    s += "declared has been given a value";
    
s += newline;
s += "typeof declared is " + typeof(declared);

// An undeclared variable cannot be compared to undefined,
// so the next line would generate an error.
// if (notDeclared == undefined) ;

s += newline;
s += "typeof notDeclared is " + typeof(notDeclared);

document.write(s);

// Output:
// declared has not been given a value
// typeof declared is undefined
// typeof notDeclared is undefined

The undefined property was introduced in Internet Explorer before Internet Explorer 6, and was made read-only in Internet Explorer 9 standards mode.

Applies To: Global Object (JavaScript)

Community Additions

Show:
© 2014 Microsoft