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
/ 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
== and the triple equality operator
== in certain circumstances.
It's basically the difference between saying "mostly equal to" (
==) and "totally exactly really really equal to" (