Booleans
There are 10 kinds of people in the world: those who understand binary and those who do not.
Last updated
There are 10 kinds of people in the world: those who understand binary and those who do not.
Last updated
By now you probably know what a boolean value is and you may have heard about boolean algebra or George Boole. Together, they are the bread and butter that makes programming work. We can compare two or more values, utilize a boolean for situations with only two states, or combine values using boolean algebra in order to control our code.
In Javascript, anything with a value is considered to be true (see truthiness). We can use comparison operators to return a true
or false
value. For example: (10 < 6) // false
.
Strongly typed languages such as C++ and Java have a boolean primitive type. JS also has a boolean type but it is loosely (or dynamically) typed, so it can take on other values as well.
Later in our programming adventure, we will even create our own functions that return a boolean value in order to state if something happened or not.
You may have seen boolean logic for logic gates. This same process can be applied to boolean values in code.
Operator
Symbol
Example(s)
Not
!
!(4 < 10)
= false
And
&&
(true && false)
= false
(3 < 5 && 7 > 1)
= true
Or
||
(true || false)
= true
(5 > 10 || 1 == 1)
true
Nand
!(x && y)
!(true && false)
= true
!(3 < 5 && 7 > 1)
= false
Nor
!(x || y)
!(true || false)
= false
!(5 > 10 || 1 == 1)
= false