Null and Undefined Types

You've met Numbers, Strings, and Booleans. There are two other types often used in JavaScript: the Null type and the Undefined type. They are special types in JavaScript, and you'll see why soon.

In this article, you will learn about the Null type and the Undefined type, their values, and how to work with them in JavaScript.

A type with only one value

You have seen that the String type can have an "infinite" number of values (within the limits of your computer memory). For example, the String type represents any of the following values.

// Examples of values with the String type
'hello, world'
"this is a string"
`Where is my pudding?`
''
'A really long string.........................................................'
    

The Number type also has this aspect. Any number that you can reasonably express in JavaScript has the Number type.

// Examples of values with the Number type
-100
99
6.28
Infinity
    

You also know about the Boolean type. It can have only two values.

// The only two values of Boolean type
true
false
    

There are not more Boolean values. You can't dream up more. There are only two, those two.

The Null type has one and exactly one value.

// The only value that has the Null type
null
    

It's just that word: null. No quotation marks. No other fancy things. Just null.

Just like the Null type, the Undefined type also has one and exactly one value.

// The only value that has the Undefined type
undefined
    

The meaning of null

This is a harder subject to tackle because it's a philosophical subject. Many people ask, "What does the value of null mean in a program?" There are a couple of answers that programmers give to this. None of them are wrong. None of them are right. They just are. In the presence of null, the code you write determines which of the following meanings null has.

During your software programming career, you will likely have all three of those opinions, sometimes at the same time. Let's take a look at some examples to try to figure this out.

Checking if a value is null

To check if a value is null, you can just compare using the strict equality operator. There is only one value of the Null data type and it's always null. Take a look at the following code and figure out what you think it will produce:

let a = 'a';
let x = null;

console.log(a === null); //=> false
console.log(x === null); //=> true
    

null is a falsey value in JavaScript. This means that if you turn null into a Boolean, it will evaluate to false.

console.log(Boolean(null)); //=> false
console.log(!null);         //=> true
    

Oh, and there's that undefined value, too

Just like the null value that is the only value of the Null data type, there is undefined which is the only value of the Undefined data type.

If you're asking yourself, "Wait, if 'null' is no value or the absence of a value, then what the heck does 'undefined' mean?", well you're not the only one.

Have a look at this code.

let value;
console.log(value); //=> undefined
    

In this code, value is declared, but it isn't assigned. It was declared as a variable, but not set to a value in the same line.

When a variable is declared, but not assigned, the variable's value will be undefined.

let value;          // declared but not assigned
value = 6.28;       // assigned or set to a value
console.log(value); //=> 6.28
    

In this code, value is declared on the first line. On the second line, it is assigned to 6.28.

So, an uninitialized variable, or a variable that has not been set to a value yet, has the value undefined by default. To test for it, you can use the strict equality operator.

// Test if a value is undefined
let value;
console.log(value === undefined); //=> true
    

undefined is also a falsey value in JavaScript.

console.log(Boolean(undefined)); //=> false
console.log(!undefined);         //=> true
    

Null vs. Undefined

Use null when you want to explicitly set the value of a variable to no value or an unknown value.

You should not explicitly set a value to undefined. undefined will automatically be the value of a variable automatically when a variable is declared, but not assigned.

What you've learned