undefined Constant (JavaScript)

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




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 shows how to use the undefined constant.

// A variable that has not been initialized.  
var declared;  

if (declared == undefined)  
    document.write("declared has not been given a value <br/>");  
    document.write("declared has been given a value <br/>");  

document.write("typeof declared is " + typeof(declared) + "<br/>");  

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

document.write("typeof notDeclared is " + typeof(notDeclared));  

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

See Also

typeof Operator