JavaScript Booleans


A JavaScript Boolean is like a switch that can be either on or off. It can only have two values: true or false.


Boolean Values

Frequently in programming, you'll require a data type that can only hold one of two values, such as:

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

To deal with this, JavaScript uses a special data type called Boolean. It can only have two possible values, which are true or false.


The Boolean() Function

You can use the Boolean() function to determine if an expression (or a variable) is true.

Or even easier:


Comparisons and Conditions

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

The section on JavaScript Conditions provides a comprehensive explanation of how conditional statements work.

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

Everything Without a Value is False


JavaScript Booleans as Objects

let x = false;

However, you can also create booleans as objects using the keyword new:

let y = new Boolean(false);

Do not create Boolean objects.

The new word makes the code harder and slower to run.

Boolean objects can produce unexpected results:

Note the difference between (x==y) and (x===y).

Comparing two JavaScript objects always return false.