Plenty of bugs come down to one simple confusion: believing that whatever fails a test equals false. Seven other values fail tests without being that boolean, and two of them, zero and the empty string, are perfectly valid data that gets wiped out by accident.
Definition
false is a literal of the language, the negative boolean value, the other one being true. It is written in lowercase, without quotes, and it is a reserved word.
You get it either by writing it, or, far more often, as the result of a comparison that does not hold.
const isPaid = false;
console.log(3 > 10); // false
console.log("a" === "b"); // false
console.log([1, 2].includes(9)); // false
console.log(typeof false); // "boolean"One detail worth noting in passing: on an empty array, [].some(...) always returns false and [].every(...) always returns true, whatever function you hand them.
The eight values treated as false
A test does not require a boolean: it converts whatever it is given. The values that turn false during that conversion are called falsy, and there are few enough of them to learn by heart.
| Value | What it stands for |
|---|---|
false | The boolean itself |
0 and -0 | The numeric zeros |
0n | Zero as a big integer |
"" | The empty string |
null | Emptiness on purpose |
undefined | Emptiness by default |
NaN | A numeric calculation that failed |
Everything else is true, without exception. An empty array, an empty object, the string "0" and the string "false" all pass a test, which contradicts intuition rather violently. The full subject is covered in Truthy and falsy.
The zero and empty string trap
This is the costliest defect on the list, because nothing breaks as long as the tested values stay large.
function showStock(quantity) {
// Trap: 0 is a valid quantity, yet falsy
return quantity || "not provided";
}
console.log(showStock(12)); // 12
console.log(showStock(0)); // "not provided", while the stock really is 0!
// Fix: only replace null and undefined
function showStockFixed(quantity) {
return quantity ?? "not provided";
}
console.log(showStockFixed(0)); // 0The same problem hits a zero percent discount, a score of zero, a billing address deliberately left blank. The practical rule: whenever a fallback concerns numeric or textual data, prefer Nullish coalescing (??) over ||.
A falsy value is not equal to false. Both fail a test, yet 0 === false is false, and so is "" === false. They behave alike, they are not identical.
Frequently asked questions
Why is an empty array not falsy?
Because an array is an object, and no object is falsy in JavaScript, whatever it contains. The list of eight false values holds primitives only. To test an array, go through its size with Array.isArray(a) && a.length > 0, which asks the real question.
What is the difference between false and null in an API response?
A considerable difference in meaning. false states a negative answer: the order is not paid. null states that there is no information: whether it is paid remains unknown. Conflating the two turns an unknown into a no, which distorts every decision made afterwards.
How do I get false from another value?
By calling Boolean(value), which applies exactly the rules in the table above, or by writing !!value, which produces the same result in two characters. This is useful before storing an answer in a database, where a real boolean is wanted rather than an empty string.