JavaScript Tutorial
JavaScript Booleans
true and false. Comparisons produce them. Boolean(value) and if (value) show what is truthy.
Two literals
A boolean is true or false. Write them in lowercase. True andFalse are Python; in JavaScript they are ordinary names, not the boolean values.
You store a yes-or-no fact: a box is checked, a score is high enough, a list still has items. Conditions consume booleans. Comparisons produce them.
Run the examples in /javascript/try. The console prints the wordstrue and false.
Comparisons produce booleans
7 > 3 is true. 7 === 3 is false. You can store that result, log it, or pass it into if in the next chapter. The comparison operators are covered in full under Comparisons.
Example
const ready = true;
const empty = false;
console.log(ready);
console.log(empty);
console.log(7 > 3);
console.log(7 === 3);
console.log(typeof ready);
document.body.textContent =
String(ready) + " " + String(7 > 3) + " " + typeof ready;
Boolean(value)
Boolean(value) converts any value to true or false using the same rule if uses. It does not change the original value. Call it when you want to seehow a value will behave as a condition.
Example
console.log(Boolean(1));
console.log(Boolean(0));
console.log(Boolean("hi"));
console.log(Boolean(""));
console.log(Boolean(null));
console.log(Boolean([]));
document.body.textContent =
String(Boolean("hi")) + " " + String(Boolean("")) + " " + String(Boolean(0));
Truthy and falsy
Values that become false in a boolean context are called falsy. Everything else is truthy. Memorize the falsy list. It is short.
| Falsy value | Notes |
|---|---|
false | The boolean itself |
0 and -0 | The number zero |
0n | Bigint zero |
"" | Empty string |
null | Intentionally empty |
undefined | No value assigned |
NaN | Not a number |
"0" is a non-empty string, so it is truthy. [] and {} are objects, so they are truthy even when they hold nothing.
if (value) uses the same rule
You do not have to write if (Boolean(value) === true). if (value) already converts. An empty string skips the block. A non-empty string runs it. 0 skips. Any other number runs.
Example
function label(value) {
if (value) {
return "truthy";
}
return "falsy";
}
console.log(label("hello"));
console.log(label(""));
console.log(label(0));
console.log(label(3));
console.log(label(undefined));
document.body.textContent =
label("hello") + " / " + label("") + " / " + label(0);
Not, and double not
!value flips a boolean: !true is false. Applied to a non-boolean, it converts first, then flips: !"" is true because the empty string is falsy.
!!value converts to boolean twice and lands on the same result as Boolean(value). You will see it in other people’s code. Boolean(value) is easier to read.
Prefer a real comparison when the value is a number you care about: if (count > 0) is clearer than if (count), because 0 is a valid count, not an error.
What to remember
- Literals are
trueandfalsein lowercase. - Comparisons produce booleans.
- The falsy list is short. Empty array and empty object are truthy.
Boolean(value)andif (value)follow that same list.
Next: comparisons — === versus ==, and why the extra equals sign matters.