Sub topic 2
The Complete List of Falsy Values
This chapter assumes you know how to declare variables with let and const, know JavaScript's primitive types, and can read if statements and console.log output. The previous chapter covered where JavaScript converts a value to a boolean. This one covers which values become false when that happens.
The eight falsy values
A falsy value is one that converts to false in a boolean context. JavaScript has a short, fixed list of them. Every value not on the list is truthy.
| Value | Type | Where it usually comes from |
|---|---|---|
| `false` | boolean | Comparisons, flags |
| `0` | number | Counters, lengths, indexes |
| `-0` | number | Arithmetic such as `-1 * 0` |
| `0n` | bigint | BigInt arithmetic |
| `""` | string | Empty input fields, `''` and empty template literals |
| `null` | null | Deliberately empty values |
| `undefined` | undefined | Missing properties, unset variables, missing arguments |
| `NaN` | number | Failed numeric conversion, such as `Number("abc")` |
You can confirm the list by passing each value to Boolean:
const falsy = [false, 0, -0, 0n, "", null, undefined, NaN];
console.log(falsy.map(Boolean));
// [false, false, false, false, false, false, false, false]Grouping the list so you remember it
The list is easier to remember when grouped by meaning:
- The boolean itself:
false. - The "zero" of each numeric type:
0,-0, and0n. - The empty string:
""with any quote style. - "No value" markers:
nullandundefined. - "Not a valid number":
NaN.
Common errors with falsy values
The most common mistake is to treat a falsy check as a "missing value" check. Code like if (count) rejects 0, and if (name) rejects "", even when those values are valid data.
function showQuantity(qty) {
if (!qty) {
return "No quantity given";
}
return `Quantity: ${qty}`;
}
console.log(showQuantity(5)); // "Quantity: 5"
console.log(showQuantity(0)); // "No quantity given" (probably a bug)
console.log(showQuantity(undefined)); // "No quantity given"A second mistake is forgetting that NaN is falsy. A failed parse silently takes the falsy branch:
const age = parseInt("twelve", 10);
console.log(age); // NaN
console.log(Boolean(age)); // false
console.log(Number.isNaN(age)); // true: the precise checkA third mistake is assuming -0 and 0n behave differently from 0 in conditions. All three are falsy. To tell -0 apart from 0, use Object.is(x, -0), because -0 === 0 is true.
Exercise
Which of these values are falsy: 0n, " ", -0, NaN, "false", null? Explain any that are not.
Show answer
0n, -0, NaN and null are falsy. " " is truthy because it contains a space, so it is not an empty string. "false" is truthy because it is a non-empty string, and its content does not matter.
Exercise
showQuantity(0) above returns "No quantity given". Rewrite the condition so that only null and undefined count as missing.
Show answer
Replace if (!qty) with if (qty == null). Loose equality with null matches only null and undefined, so showQuantity(0) now returns "Quantity: 0".