A boolean is the simplest data type there is: it has exactly two possible values, true and false. Booleans are the language of decisions. Every if, every loop, and every comparison comes down to one of these two values.
Programs constantly need to represent one of two states:
isMember)isMuted)hasPassed)let isOnline = true;
let isLoggedIn = false;
console.log(typeof isOnline);
Two rules for writing them:
true and false must be lowercase. True or FALSE is not a boolean; JavaScript reads it as a variable name, and you get a ReferenceErrortrue and false are written without quotes. "true" is a four-letter string, not a booleanisOpen, hasTicket, canVote. Then if (canVote) reads almost like English.
You rarely type true or false directly. Most booleans come from comparisons, and every comparison operator gives one back. Given let x = 5;:
| Description | Example | Returns |
|---|---|---|
| Equal to | x === 8 | false |
| Not equal to | x !== 8 | true |
| Greater than | x > 8 | false |
| Less than | x < 8 | true |
You can store the result of a comparison in a variable, and use it later:
const score = 85;
const passed = score >= 60;
const excellent = score >= 90;
if (passed) {
console.log("You passed! Excellent: " + excellent);
}
Booleans drive every kind of decision in JavaScript. You have seen conditions; loops (coming up in the next section) use them too, to decide whether to keep repeating:
| Where | Example | The boolean decides |
|---|---|---|
| if | if (age < 18) | whether the block runs |
| ternary | hour < 18 ? "day" : "evening" | which value is picked |
| for loop | for (let i = 0; i < 5; i++) | whether to go around again |
| while loop | while (lives > 0) | whether to keep going |
Boolean() converts any value to true or false. It is useful for checking how JavaScript will treat a value in a condition:
console.log(Boolean(10 > 9));
console.log(10 > 9); // even easier: a comparison already is a boolean
console.log(Boolean("hello"));
console.log(Boolean(0));
When JavaScript turns a non-boolean value into a boolean, anything that has a real "value" becomes true. These are called truthy:
100 // true
3.14 // true
-15 // true: any non-zero number, even negative
"Hello" // true
"false" // true: it is a non-empty string!
"0" // true: also a non-empty string
" " // true: a space is a character
(7 + 1 + 3.14) // true: the result is 11.14
[] // true: an empty array
{} // true: an empty object
The last two surprise people. Arrays and objects are always truthy, no matter what is (or is not) inside them, because they are objects, and every object is truthy.
There are exactly eight falsy values. Memorize this list; everything not on it is truthy.
| Value | Type | Notes |
|---|---|---|
| false | boolean | The boolean itself |
| 0 | number | Zero |
| -0 | number | Minus zero |
| 0n | bigint | BigInt zero |
| "" | string | Empty string only; " " (a space) is truthy |
| null | null | Empty on purpose |
| undefined | undefined | A variable never given a value |
| NaN | number | Failed math, like 10 / "Hallo" |
let x; // undefined
console.log(Boolean(x));
console.log(Boolean(10 / "Hallo")); // NaN
console.log(Boolean(""));
Normally a boolean is a simple primitive value: let x = false;. But JavaScript also lets you create a boolean object with the new keyword. You should never do this, but you should recognize it, because it behaves very strangely:
let x = false;
let y = new Boolean(false);
console.log(typeof x);
console.log(typeof y);
console.log(x == y);
console.log(x === y);
if (y) {
console.log("This runs, even though y holds false!");
}
y is an object wrapping the value false, and every object is truthy. So if (y) runs, which is exactly backwards. Boolean objects also make code slower and cannot be compared reliably with ===. Never use new Boolean(). Plain Boolean(value) without new is fine: it returns a normal primitive boolean.
Which of these is falsy?
"false"[]-1NaNNaN is one of the eight falsy values. "false" is a non-empty string, [] is an object, and -1 is a non-zero number, so all three are truthy.What does this log?
let answer = "false";
if (answer) {
console.log("yes");
} else {
console.log("no");
}
yes. "false" in quotes is a five-letter string, and any non-empty string is truthy. This happens in real code when a true/false setting is stored or sent as text.Which is a correctly written boolean?
let open = True;let open = "true";let open = true;let open = new Boolean(true);Boolean() on at least eight different values. Predict each answer before you click Run" " (a space) is truthy but "" is falsyconst canVote = age >= 18;, and use it in an ifnew Boolean(false), put it in an if, and see it run. Then try the same with Boolean(false) (no new)