JavaScript: undefined

By Xah Lee. Date: . Last updated: .

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:

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);

JavaScript Special Literals

BUY ΣJS JavaScript in Depth