JavaScript: undefined
undefined
is the value of the property key "undefined"
of
the Global Object.
console.log( globalThis.hasOwnProperty("undefined"), globalThis["undefined"] === undefined, );
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");
undefined
is the value when:
- Variable doesn't have a value (not initialized). [see let Declaration]
- 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 xx; console.log(xx === undefined);
// accessing non-existent property return undefined console.log({ "kk": 3 }.kk === 3); console.log({ "kk": 3 }.xx === undefined);
// accessing out-of-bound array element returns undefined const aa = [1, 2]; console.log(aa[9] === undefined);
// When function argument is not given, value is undefined function ff(x) { return x; } console.log(ff() === undefined);
// undefined is the return value of a function call when the function doesn't have return statement function ff() {} console.log(ff() === undefined);
// undefined is the return value of a function with empty return statement function ff() { return; } console.log(ff() === undefined);