JavaScript Booleans
In JavaScript, a Boolean is a primitive data type that can only have one of two values:
The Boolean Data Type
In JavaScript, a Boolean is a primitive data type that can only have one of two values:
true or false
The Boolean value of an expression is the basis for all JavaScript comparisons and conditions.
Key Boolean Characteristics
- true and false are boolean data types
- true and false are the only possible boolean values
- true and false must be written in lowercase
- true and false must be written without quotes
Boolean Use Cases
Very often, in programming, you will need a data type that can represent one of two values, like:
- yes or no
- on or off
- true or false
Boolean values are fundamental for logical operations and control flow in JavaScript programming.
Comparisons
All JavaScript comparison operators (like ==, !=, ) return true or false from the comparison.
Given that x = 5, the table below explains comparison:
| Description | Example | Returns |
|---|---|---|
| Equal to | (x == 8) | false |
| Not equal to | (x != 8) | true |
| Greater than | (x > 8) | false |
| Less than |
(x See Also: > JavaScript Comparisons
Conditions
Booleans are extensively used in if statements to determine the code blocks to execute based on the logic.
| Example | Result |
|---|---|
| if (day == "Monday") | true or false |
| if (salary > 9000) | true or false |
| if (age See Also: > JavaScript if > JavaScript if else
Loops
Booleans are extensively used in loops to determine conditions for looping.
| Description | Example |
|---|---|
| For loop |
for (i = 0; i See Also: > JavaScript Loops
The Boolean() Function
You can use the Boolean() function to find out if an expression (or a variable) is true:
Example
Example Code
javascriptBoolean(10 > 9)Or even easier:
Example
Example Code
javascript(10 > 9)Everything With a "Value" is True
Examples
Example Code
javascript100 is true
3.14 is true
-15 is true
true is true
"Hello" is true
"false" is true
(7 + 1 + 3.14) is true
[ ] is true
{ } is trueEverything Without a "Value" is False
Examples
Example Code
javascript0 is false
"" is false
undefined is false
null is false
NaN is false
false is falseExample
Example Code
javascriptlet x = 0;
Boolean(x);Example
Example Code
javascriptlet x = -0;
Boolean(x);Example
Example Code
javascriptlet x = "";
Boolean(x);Example
Example Code
javascriptlet x;
Boolean(x);Example
Example Code
javascriptlet x = null;
Boolean(x);Example
Example Code
javascriptlet x = false;
Boolean(x);Example
Example Code
javascriptlet x = 10 / "Hallo";
Boolean(x);JavaScript Booleans as Objects
Normally JavaScript booleans are primitive values created from literals:
Example
Example Code
javascriptlet x = false;But booleans can also be defined as objects with the keyword new:
Example
Example Code
javascriptlet y = new Boolean(false);Example
Example Code
javascriptlet x = false;
let y = new Boolean(false);
//
typeof x returns boolean
//
typeof y returns objectWarning
Do not create Boolean objects.
The new keyword complicates the code and slows down execution speed.
Boolean objects can produce unexpected results:
Example
Example Code
javascriptlet x = Boolean(false);
let y = new Boolean(false);
(x == y) returns true
(x === y) returns falseComparing two JavaScript objects always returns false.
Interactive Code Examples
JS Example 1
javascriptlet x = 5;
(x == 8); // equals false
(x != 8); // equals trueJS Example 2
javascriptif (hour < 18) {
greeting = "Good day";
} else {
greeting = "Good evening";
}JS Example 3
javascriptwhile (i < 10) {
text += i;
i++;
}Examples
javascript100 is true
3.14 is true
-15 is true
true is true
"Hello" is true
"false" is true
(7 + 1 + 3.14) is true
[ ] is true
{ } is true