JavaScript: “undefined”

By Xah Lee. Date: . Last updated: .

What is 「undefined」 in JavaScript?

JavaScript has a literal value undefined. For example, you can write var x = undefined;

undefined is one of the datatype of JavaScript, and it is the only value of that type. 〔►see JavaScript: Data Types

undefined is the value when:

  1. Variable doesn't have a value (not initialized). 〔►see JavaScript Variable Scope: Function Level vs Block Level
  2. Accessing a non-existent property. 〔►see JavaScript: Read/Write to Property and Prototype Chain
  3. Accessing array element with index out of bound, or non-existent element in a sparse array. 〔►see JavaScript: Understand JS Array
  4. When function argument is not given. 〔►see JavaScript: Function Parameters
  5. Return value of a function call when the function doesn't have return statement, or empty return statement. 〔►see JavaScript: 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 JavaScript: What's “null” Used For?

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

Like what you read? Buy JavaScript in Depth
or, buy a new keyboard, see Keyboard Reviews.