THE WORLD'S LARGEST WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

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
Try it yourself »

Or even easier:

Example

(10 > 9)              // also returns true
10 > 9                // also returns true
Try it yourself »

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)

Note The Boolean value of an expression is the fundament for JavaScript comparisons and conditions.


Everything With a "Real" Value is True

Examples

100

3.14

-15

"Hello"

"false"

7 + 1 + 3.14

5 < 6
Try it yourself »

Everything Without a "Real" is False

The Boolean value of 0 (zero) is false:

var x = 0;
Boolean(x);       // returns false
Try it yourself »

The Boolean value of -0 (minus zero) is false:

var x = -0;
Boolean(x);       // returns false
Try it yourself »

The Boolean value of "" (empty string) is false:

var x = "";
Boolean(x);       // returns false
Try it yourself »

The Boolean value of undefined is false:

var x;
Boolean(x);       // returns false
Try it yourself »

The Boolean value of null is false:

var x = null;
Boolean(x);       // returns false
Try it yourself »

The Boolean value of false is (you guessed it) false:

var x = false;
Boolean(x);       // returns false
Try it yourself »

The Boolean value of NaN is false:

var x = 10 / "H";
Boolean(x);       // returns false
Try it yourself »

Boolean Properties and Methods

Primitive values, like true and false, cannot have properties or methods (because they are not objects).

But with JavaScript, methods and properties are also available to primitive values, because JavaScript treats primitive values as objects when executing methods and properties.


Complete Boolean Reference

For a complete reference, go to our Complete JavaScript Boolean Reference.

The reference contains descriptions and examples of all Boolean properties and methods.