Day 1: Simple Types
Lesson 6: Boolean
The boolean data type has a fancy name for a simple concept. Is something true or is it false?
1const iAmHuman = true; 2const theSunIsCold = false;
To really understand the power of this data type we need to look at Boolean Operators. These are like the addition +
, subtraction -
, division /
and multiplication *
operators you saw earlier but for this boolean data type. They are:
1let greaterThan = 4 > 3; 2let lessThan = 3 < 4; 3let greaterThanEqualTo = 4 >= 3; 4let lessThanEqualTo = 3 < 4; 5let equalTo = 3 == 3; 6let notEqualTo = 3 != 4; 7let exactlyEqualTo = 3 === 3;
All of the lines above will set the variable on the left of the first =
sign to be true because the test that they apply is true. If we change the first line to let greaterThan = 1 > 3;
then greaterThan
will be set to false because the 1 is not greater than 3.
For not equal to !=
we simply take the equals to operator ==
and replace the first =
with the not operator !
.
Let's look at some more examples:
You can see in the console which test returns true and which returns false.
Use the playground below to practice a few other comparisons.
One last thing
==
vs ===
One of the downsides about JavaScript's history means it has some quirks that have developed over time. One of those is the double equality operator ==
and the triple equality operator ===
.
I'm not going to go into detail here on this because it's waaaaaay too early in you JavaScript journey, if you are brand new to coding it's doubly too early. For now just accept that you should pretty much always use ===
in JavaScript and only use ==
in certain circumstances.
It's basically the difference between saying "mostly equal to" (==
) and "totally exactly really really equal to" (===
)