In programming, you frequently require a data type that can have one of two values, such as
For this purpose, JavaScript provides the Boolean data type, which can only be true or false.
You can use the Boolean() function to determine whether an expression (or variable) evaluates to true.
Boolean(10 > 9) |
Or even more simply:
(10 > 9) 10 > 9 |
The chapter on JS Comparisons provides a comprehensive overview of comparison operators.
The chapter on JS If Else offers an in-depth look at conditional statements.
Here are a few examples:
Operator |
Description |
Example |
== |
equal to |
if (day == “Monday”) |
> |
greater than |
if (salary > 9000) |
< |
less than |
if (age < 18) |
100 3.14 –15 “Hello” “false” 7 + 1 + 3.14 |
The Boolean value of 0 (zero) is considered false.
let x = 0; Boolean(x); |
The Boolean value of -0 (minus zero) is also considered false.
let x = –0; Boolean(x); |
The Boolean value of an empty string (“”) is evaluated as false.
let x = “”; Boolean(x); |
The Boolean value of undefined is regarded as false.
let x; Boolean(x); |
The Boolean value of null is considered false.
let x = null; Boolean(x); |
The Boolean value of false is, as expected, false.
let x = false; Boolean(x); |
The Boolean value of NaN is false:
let x = 10 / “Hallo”; Boolean(x); |