JavaScript for impatient programmers
Please support this book: buy it or donate
(Ad, please don’t block.)

13 The non-values undefined and null

Many programming languages have one “non-value” called null. It indicates that a variable does not currently point to an object – for example, when it hasn’t been initialized yet.

In contrast, JavaScript has two of them: undefined and null.

13.1 undefined vs. null

Both values are very similar and often used interchangeably. How they differ is therefore subtle. The language itself makes the following distinction:

Programmers may make the following distinction:

13.2 Occurrences of undefined and null

The following subsections describe where undefined and null appear in the language. We’ll encounter several mechanisms that are explained in more detail later in this book.

13.2.1 Occurrences of undefined

Uninitialized variable myVar:

let myVar;
assert.equal(myVar, undefined);

Parameter x is not provided:

function func(x) {
  return x;
assert.equal(func(), undefined);

Property .unknownProp is missing:

const obj = {};
assert.equal(obj.unknownProp, undefined);

If you don’t explicitly specify the result of a function via a return statement, JavaScript returns undefined for you:

function func() {}
assert.equal(func(), undefined);

13.2.2 Occurrences of null

The prototype of an object is either an object or, at the end of a chain of prototypes, null. Object.prototype does not have a prototype:

> Object.getPrototypeOf(Object.prototype)

If you match a regular expression (such as /a/) against a string (such as 'x'), you either get an object with matching data (if matching was successful) or null (if matching failed):

> /a/.exec('x')

The JSON data format does not support undefined, only null:

> JSON.stringify({a: undefined, b: null})

13.3 Checking for undefined or null

Checking for either:

if (x === null) ···
if (x === undefined) ···

Does x have a value?

if (x !== undefined && x !== null) {
  // ···
if (x) { // truthy?
  // x is neither: undefined, null, false, 0, NaN, ''

Is x either undefined or null?

if (x === undefined || x === null) {
  // ···
if (!x) { // falsy?
  // x is: undefined, null, false, 0, NaN, ''

Truthy means “is true if coerced to boolean”. Falsy means “is false if coerced to boolean”. Both concepts are explained properly in §14.2 “Falsy and truthy values”.

13.4 undefined and null don’t have properties

undefined and null are the two only JavaScript values where you get an exception if you try to read a property. To explore this phenomenon, let’s use the following function, which reads (“gets”) property .foo and returns the result.

function getFoo(x) {

If we apply getFoo() to various values, we can see that it only fails for undefined and null:

> getFoo(undefined)
TypeError: Cannot read property 'foo' of undefined
> getFoo(null)
TypeError: Cannot read property 'foo' of null

> getFoo(true)
> getFoo({})

13.5 The history of undefined and null

In Java (which inspired many aspects of JavaScript), initialization values depend on the static type of a variable:

In JavaScript, each variable can hold both object values and primitive values. Therefore, if null means “not an object”, JavaScript also needs an initialization value that means “neither an object nor a primitive value”. That initialization value is undefined.


See quiz app.