Sub topic 8
Truthy and Falsy Values
This chapter assumes you know the seven primitive types and the object type. It also assumes you have seen the difference between explicit and implicit coercion. The topic here is ToBoolean, the abstract operation JavaScript runs whenever it needs a value to be true or false.
Where ToBoolean Runs
ToBoolean runs explicitly when you call Boolean(value) or write !!value. It runs implicitly in every boolean context:
if,while,do...while, and the test part of aforloop- the condition of the ternary operator
? : - the operand of
! - the left operand of
||and&&, which they test to decide which operand to return
ToBoolean never fails and never calls your code. It does not call valueOf, toString, or Symbol.toPrimitive. Each value maps straight to a boolean according to a fixed table.
The Falsy List
A value is falsy if ToBoolean turns it into false. Only these values are falsy:
| Value | Type |
|---|---|
| `false` | boolean |
| `0`, `-0` | number |
| `0n` | bigint |
| `""` | string (empty) |
| `null` | null |
| `undefined` | undefined |
| `NaN` | number |
Every other value is truthy. Every object is truthy too, however empty it is.
Surprising Truthy Values
Most bugs come from values that look empty but are truthy:
console.log(Boolean("0")); // true: non-empty string
console.log(Boolean("false")); // true: non-empty string
console.log(Boolean(" ")); // true: a space is a character
console.log(Boolean([])); // true: every object is truthy
console.log(Boolean({})); // true
console.log(Boolean(new Boolean(false))); // true: a wrapper object
console.log(Boolean(-1)); // true: only 0 and -0 are falsy numbers
console.log(Boolean(Infinity)); // trueCommon Errors
The usual mistake is using a truthiness test when one of the falsy values is valid data:
function describeStock(quantity) {
if (!quantity) {
return "unknown"; // wrong for 0, which is real data
}
return `${quantity} in stock`;
}
console.log(describeStock(0)); // "unknown": a bug
function describeStockFixed(quantity) {
if (quantity === undefined || quantity === null) {
return "unknown";
}
return `${quantity} in stock`;
}
console.log(describeStockFixed(0)); // "0 in stock"Empty arrays cause the opposite mistake. if (items) passes for [], so test items.length > 0 when you need to know whether there is content.
Exercises
Exercise
Which of these are truthy: "0", 0n, [], NaN, " ", -0, {}?
Show answer
"0", [], " ", and {} are truthy. 0n, NaN, and -0 are on the falsy list.
Exercise
A form handler runs if (!input.value) showError(). Users who type 0 into a numeric field see an error. If the value is the string "0", is truthiness the cause? What if the code first converts the input to a number?
Show answer
The string "0" is truthy, so !input.value is false and the check alone does not cause the error. After Number(input.value) the value becomes 0, which is falsy, and the error appears. Check for an empty string before converting, or test the number with Number.isNaN instead of relying on truthiness.