JavaScript defines two reserved words for booleans: true
and false
.
Those are used to create a boolean value.
You can create a boolean using the Boolean() factory function:
const isReal = Boolean(true)
or using the boolean literal syntax:
const isReal = true
Booleans are great for comparisons and to control the flow of a program.
Comparison operations ==
===
<
>
(and so on) return either true or false.
Example:
const a = 1
a === 1 //true
if
, while
statements and other control structures use booleans to determine the flow of the program.
Truthy and falsy values
They don’t just accept true or false, but also accept truthy and falsy values.
Falsy values, values interpreted as false, are
0
-0
NaN
undefined
null
'' //empty string
All the rest is considered a truthy value.