Booleans, null and undefined

A boolean is a yes-or-no value. There are only two booleans: true and false. You write them without quotes. A comparison gives you a boolean:

const passed = 95 > 90;
console.log(passed);
console.log(3 > 5);

Output:

true
false

Booleans are what if statements and loops use to make decisions. You will meet those in the Control Flow section.

Two kinds of "nothing"

JavaScript has two values that mean "no value": undefined and null.

undefined means that nobody has given this a value yet. A variable that you declare without a value holds undefined:

let winner;
console.log(winner);

Output:

undefined

You will see undefined again later, for example when you ask an object for a property it does not have. If you get undefined when you did not expect it, something was not set, and that is often a bug.

null means "empty on purpose". JavaScript never sets it for you. You write it yourself, as in const prize = null;. This says that there is no prize, and that this is a decision, not a mistake.

Checking types with typeof

The typeof operator tells you the type of a value, as a string:

console.log(typeof true);
console.log(typeof undefined);
console.log(typeof null);

Output:

boolean
undefined
object

The last line is wrong. null is not an object. This is a mistake from the first version of JavaScript in 1995. Many programs already depended on it, so it was never fixed. JavaScript keeps old mistakes like this so that old code does not break. To check whether a value is null, compare it with null instead of using typeof. You will learn how in the Operators section.

In the exercise, print a comparison, an unset variable, and the type of three values, including null.