Sub topic 6
Truthy and Falsy Values
This chapter assumes you know JavaScript's primitive types, can declare variables with let and const, and can run code in a browser console or Node.js. It builds on the earlier chapter on type coercion, where ToBoolean was introduced as one of the conversion steps.
The working model
When JavaScript needs a boolean but gets some other value, it applies ToBoolean. This happens in if and while conditions, in the ternary ? :, with the ! operator, and when you call Boolean(value). ToBoolean doesn't look at the value's content beyond its type and a few special cases. It checks a short, fixed list.
A falsy value is one that ToBoolean turns into false. A truthy value is any value that is not falsy. Since the falsy list is short, the easy approach is to memorize it and treat every other value as truthy.
| Falsy value | Type |
|---|---|
| `false` | boolean |
| `0`, `-0` | number |
| `0n` | bigint |
| `""` (empty string) | string |
| `null` | null |
| `undefined` | undefined |
| `NaN` | number |
Browsers have one legacy exception: document.all is an object that is falsy. It exists only for old web compatibility, and you can ignore it in new code.
Seeing it in code
Boolean(x) and !!x both show the result of ToBoolean. The double ! negates the value twice and leaves you with a plain boolean.
const values = [false, 0, -0, 0n, '', null, undefined, NaN,
'0', 'false', ' ', [], {}, function () {}, new Boolean(false)];
for (const v of values) {
console.log(String(v).padEnd(20), Boolean(v), !!v);
}
// The first eight print false; the remaining seven print true.Common errors
Strings that look false are truthy. '0', 'false', and ' ' are non-empty strings, so they are truthy. Form inputs and query parameters arrive as strings, which means if (params.enabled) passes when the value is 'false'. Compare explicitly with params.enabled === 'true'.
Empty containers are truthy. [] and {} are objects, and every object is truthy. To test for emptiness, check arr.length === 0 or Object.keys(obj).length === 0.
Wrapper objects are truthy. new Boolean(false) is an object, so if (new Boolean(false)) runs its body. Don't create primitive wrappers with new.
Valid zeros get rejected. Code such as if (count) treats 0 the same as "missing". When 0 or '' is a legitimate value, test for what you actually mean:
function describe(count) {
if (count === undefined || count === null) return 'no data';
return `${count} items`;
}
console.log(describe(0)); // '0 items'
console.log(describe(undefined)); // 'no data'Later chapters build on these rules. The && and || operators use truthiness to decide which operand to return, and ?? exists because truthiness checks reject valid values like 0.
Exercise
Which of these values are truthy: '0', 0n, [], NaN, -0, {}, ' ', null?
Show answer
'0', [], {}, and ' ' are truthy. Non-empty strings and all objects are truthy. 0n, NaN, -0, and null are on the falsy list.
Exercise
A function runs if (!user.age) return 'Age required';. Why does it reject a newborn whose age is 0, and how would you fix it?
Show answer
0 is falsy, so !0 is true and the function returns early. Check for a missing value directly instead: if (user.age === undefined || user.age === null). You could also use user.age == null, which matches only null and undefined.