Chapter IV · Operators & Expressions
Truthy and Falsy Values
Every value in JavaScript is either "truthy" or "falsy" when used where a boolean is expected — and there are exactly eight falsy values, everything else is truthy.
01 What Are Truthy and Falsy Values?
Conditions in if, while, and logical operators don't require an actual boolean — they coerce whatever they're given into one. This exists so you can write if (user) instead of if (user !== null && user !== undefined).
The falsy values are a short, memorizable list: false, 0, -0, 0n, '', null, undefined, NaN. Everything else — including '0' the string, empty arrays [], and empty objects {} — is truthy.
02 Simple Example
The full falsy list, and a couple of common surprises.
if (0) console.log('never runs');
if ('0') console.log('this runs — non-empty string is truthy');
if ([]) console.log('this runs — even an empty array is truthy');
03 How It Works
When a value appears where a boolean is required, the engine runs it through the abstract ToBoolean operation from the spec — a lookup, not a calculation. It checks whether the value matches one of the eight falsy cases; if not, the result is true.
04 Production Example
Guarding against missing or empty data before rendering it.
function renderCartSummary(items) {
if (!items || items.length === 0) {
return 'Your cart is empty.';
}
return `${items.length} item(s) in your cart.`;
}
05 Mental Model
"A short blocklist, not a checklist: JavaScript only treats eight specific values as 'off' — everything else defaults to 'on.'"
06 Common Mistakes
- Assuming empty arrays/objects are falsy — they aren't; check
.lengthorObject.keys().lengthexplicitly. - Using
if (count)to check "has a value" when0is a valid, meaningful count — that condition silently treats 0 as missing.
07 Interview Questions
Q: Name all eight falsy values.
false, 0, -0, 0n, '', null, undefined, and NaN — every other value, including [] and {}, is truthy.
Q: Why is if (count) risky when count could be 0?
It silently treats a real, meaningful 0 the same as missing data — use an explicit check like count !== undefined when 0 is valid.
Key Takeaway
Only eight values are falsy (false, 0, -0, 0n, "", null, undefined, NaN); everything else, including [] and {}, is truthy — worth checking explicitly when 0 or "" are meaningful data.