JS Booleans

JavaScript Booleans


A JavaScript Boolean represents one of two values: true or false.


Boolean Values

Very often, in programming, you will need a data type that can only have one of two values, like

  • YES / NO
  • ON / OFF
  • TRUE / FALSE

For this, JavaScript has a Boolean data type. It can only take the values true or false.


The Boolean() Function

You can use the Boolean() function to find out if an expression (or a variable) is true:

Example

Boolean(10 > 9)        // returns true

Or even easier:

Example

(10 > 9)              // also returns true
10 > 9                // also returns true

Comparisons and Conditions

The chapter JS Comparisons gives a full overview of comparison operators.

The chapter JS Conditions gives a full overview of conditional statements.

Here are some examples:

Operator Description Example
== equal to if (day == "Monday")
> greater than if (salary > 9000)
< less than if (age < 18)

The Boolean value of an expression is the basis for all JavaScript comparisons and conditions.



Everything With a "Value" is True

Examples

100

3.14

-15

"Hello"

"false"

7 + 1 + 3.14

Everything Without a "Value" is False

The Boolean value of 0 (zero) is false:

let x = 0;
Boolean(x);       // returns false