JS: undefined

By Xah Lee. Date: . Last updated: .

undefined is a builtin literal value. For example, you can write var x = undefined;

Type of undefined is "undefined", and it is the only value of the type.

〔►see JS: Data Types

console.log ( typeof undefined === "undefined" ); // true

undefined is the value when:

  1. Variable doesn't have a value (not initialized). 〔►see JS: var Name Scope
  2. Accessing a non-existent property. 〔►see JS: Get Property, Set Property
  3. Accessing array element with index out of bound, or non-existent element in a sparse array. 〔►see JS: Understand JS Array
  4. When function argument is not given. 〔►see JS: Function Parameters
  5. Return value of a function call when the function doesn't have return statement, or empty return statement. 〔►see JS: Functional Programing
  6. other similar cases.
var x;
console.log(x); // undefined
// accessing non-existent property returns undefined
var o = {"p":1};
console.log(
    o.b
); // undefined
// accessing out-of-bound array element returns undefined
var arr = ["a", "b"];
console.log(
    arr[9]
); // undefined
// When function argument is not given, value is undefined
function f(x) { return x; };

console.log(
    f()
); // undefined
// undefined is the return value of a function call when the function doesn't have return statement
function f() {};
console.log(
    f()
); // undefined
// undefined is the return value of a function with empty return statement
function f() {return;};
console.log(
    f()
); // undefined

〔►see JS: null

ECMAScript 2015 §ECMAScript Data Types and Values#sec-ecmascript-language-types-undefined-type

Like what you read? Buy JavaScript in Depth