JavaScript Booleans
A JavaScript Boolean represents one of two values: true or false.
Boolean Values
CompletedVery 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
CompletedYou can use the Boolean() function to find out if an expression (or a variable) is true:
Or even easier:
Comparisons and Conditions
CompletedThe 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) |
Everything With a "Real" Value is True
CompletedEverything Without a "Real" is False
CompletedThe 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 false is (you guessed it) false:
var x = false;
Boolean(x); // returns false
Try it Yourself
Boolean Properties and Methods
CompletedPrimitive 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
CompletedFor a complete reference, go to our Complete JavaScript Boolean Reference.
The reference contains descriptions and examples of all Boolean properties and methods.
