A value that has never been defined, such as a variable that has not been initialized.
Syntaxundefined
Examples
Usage of undefined with strict equality.
var foo;
if (foo === undefined) {
} else {
}
Usage of undefined
with the typeof operator.
var foo;
if (typeof foo === 'undefined') {
} else {
}
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
.
if (typeof foo === "undefined") {
}
if (foo === undefined) {
}
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
.
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
Microsoft Developer Network: Article
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4