JavaScript: undefined
undefined
is the value of the property key "undefined"
of
the Global Object.
console.log( window["undefined" ] === undefined ); // true
undefined
is a literal value. For example, you can write let x = undefined;
Type of undefined
is "undefined"
, and it is the only value of the type.
console.log( typeof undefined === "undefined" ); // true
undefined
is the value when:
- Variable doesn't have a value (not initialized). [see var Name Scope]
- Accessing a non-existent property. [see Get Property, Set Property]
- Accessing array element with index out of bound, or non-existent element in a Sparse Array.
- When function argument is not given. [see Function Parameters]
- Return value of a function call when the function doesn't have
return
statement, or emptyreturn
statement. - other similar cases.
let x; console.log( x === undefined ); // true
// accessing non-existent property returns undefined const o = {"p":1}; console.log( o.b === undefined ); // true
// accessing out-of-bound array element returns undefined const arr = ["a", "b"]; console.log( arr[9] === undefined ); // true
// When function argument is not given, value is undefined function f(x) { return x; }; console.log( f() === undefined ); // true
// undefined is the return value of a function call when the function doesn't have return statement function f() {}; console.log( f() === undefined ); // true
// undefined is the return value of a function with empty return statement function f() {return;}; console.log( f() === undefined ); // true
[see null]