Understanding data types: boolean and equality

Updated: May 27, 2021

These notes are taken from Beginner JavaScript by Wes Bos. Go buy it!

A boolean value is either true or false. Like a light switch it’s either on or off.

Booleans are used for logic, such as if statements. They can be calculated or manually set.

let isDrawing = false;

In this case we have a manually set flag variable which might describe the users actions on a page.

We can also calculate booleans:

// set the age
let age = 18;
// age is greater than 19
const ofAge = age > 19;
// returns boolean value of false
console.log(ofAge);

Understanding equality #

We use one equal sign operator (=) to set a variable.

const age = 100;

We also have double equals (==) and triple equals (===). But, what’s the difference? And which one should we use?

Double equals (==) checks the value of the left and right are the same. However it will ignore differences in type.

Take this example. Both left and right values equal 10, however even though the left value is of type string and the right is a number it still returns true.

"10" == 10;
true;

In contrast, triple equals (===) will always check that both the value and the type of the left and right equal the same

The example below returns true as the value of the left and right is 10. And both data types are the same. In this case they are both numbers.

10 === 10;
true;

Next take the following example:

"10" === 10;
false;

Here the values are the same, but the types differ. The left is a string and the right is a number. So false is returned.

You should almost always use triple equals.


Reply by email

Monthly Newsletter

Once a month I curate a newletter for designers and developers interested in static sites, CSS and web performance. Check out past issues to get an idea.