Skip to content
Theory · Variables and types · lesson 6 of 9

null and undefined

Back to the storeroom. A box can turn out empty for two reasons. Nothing has been put in one yet: nobody has got round to it. In the other, someone put a note saying "empty here": they checked and decided it should be that way. JavaScript has two different values for these two cases.

A variable can be declared with let and given no value. If you read such a variable, the program does not stop with an error:

JavaScript
let answer;
console.log(answer);  // undefined
answer = 42;
console.log(answer);  // 42

Let's go through the example line by line:

  1. let answer; declares a variable without a value.
  2. console.log(answer); prints undefined.
  3. answer = 42; puts a number in the variable.
  4. The second console.log(answer); prints 42.

Before the assignment, answer already holds something — the value undefined. In JavaScript two values mean "no value": undefined and null. They come about in different ways.

Here the empty-box analogy stops working. A real empty box contains nothing, while undefined and null are values themselves. They can be read, printed and put in another variable.

Where undefined comes from

JavaScript puts in undefined by itself when no value was given. There are three such cases.

A variable without a value

A variable declared with let without = holds undefined until something is assigned to it. This is answer from the example above.

A missing property

A property is a value stored inside another value under its own name. A dot after a name reads a property: Math.PI is the number π, and Math.sqrt is a function that computes a square root. If there is no property with that name, the dot gives undefined, not an error:

JavaScript
console.log(Math.PI);  // 3.141592653589793
console.log(Math.pi);  // undefined

A function without return

If a function has no return, calling it still returns a value, and that value is undefined:

JavaScript
function show(x) {
    console.log(x);
}

const result = show(5);  // 5
console.log(result);  // undefined

The call show(5) prints 5 inside the function. The function has nothing to return, so result gets undefined.

An undeclared name is an error

A variable declared without a value exists and holds undefined. A name that is not declared anywhere is different: if the program reads such a name, it stops with a ReferenceError.

The program sets null

JavaScript never puts null in place of a missing value by itself: the program's code stores null in a variable to say explicitly "this is empty". This is the note in the box.

For example, the winner of a game is not known yet, and the program writes let winner = null;. When the winner is decided, the variable is assigned a string with their name. A reader of the code sees that the empty value is intended here, not forgotten.

So undefined means "no value was given", while null means "there is no value, on purpose". Even their typeof differs: "undefined" for undefined and "object" for null.

Interviewers like to ask this: "How does null differ from undefined?" The answer has three parts:

  • JavaScript puts in undefined by itself: for a variable without a value, a missing property, a function without return;
  • null never appears by itself — code sets it on purpose;
  • typeof undefined gives "undefined", while typeof null gives "object".

Common mistake

A property name was typed with the wrong letters, Math.pi instead of Math.PI:

JavaScript
const radius = 2;
const length = 2 * Math.pi * radius;  // ✗
console.log(length);  // NaN

There is no error: Math.pi gives undefined, and multiplying by undefined gives NaN. The program does not stop, and NaN moves on into later calculations, so such a typo is hard to notice. JavaScript tells capital and small letters apart. The fixed line is const length = 2 * Math.PI * radius;.

Key points

  • JavaScript puts in undefined by itself: for a variable without a value, a missing property and a function without return.
  • Code sets null to say explicitly "there is no value".
  • typeof undefined is "undefined", and typeof null is "object".
  • Reading an undeclared name stops the program with a ReferenceError instead of giving undefined.

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way