Full answer
In an if, a while, a ternary, or an operand of !, &&, and ||, the value does not have to be a boolean. The engine converts it with the same rules as Boolean(value). The falsy values are a short fixed list: false, 0, -0, 0n (the zero BigInt), "" (empty string), null, undefined, and NaN. The rare object document.all in browsers is also falsy for historical reasons. All other values are truthy.
Traps come from values that look empty. An empty array [], an empty object {}, the string "0", the string "false", and an instance of new Boolean(false) are all truthy. Another trap is using a truthiness check for numbers: if (count) treats a valid 0 as missing.
The operators && and || do not return booleans. They return one of their operands. a || b returns a if it is truthy, otherwise b. a && b returns a if it is falsy, otherwise b. This is used for defaults and guards. When 0 or "" are valid values, use ??, which falls back only for null and undefined.
!!value is a short way to convert to a boolean, and Boolean(value) says the same thing more clearly.
const values = [false, 0, -0, 0n, '', null, undefined, NaN, [], {}, '0', 'false', ' ', -1, Infinity];
for (const value of values) {
console.log(String(typeof value).padEnd(9), JSON.stringify(String(value)).padEnd(12), Boolean(value));
}
const quantity = 0;
console.log(quantity || 10, quantity ?? 10);
const name = '';
console.log(name || 'guest', name ?? 'guest');
const user = { profile: null };
console.log(user.profile && user.profile.name);
console.log(1 && 'second', 0 && 'second', null || 'fallback', 'first' || 'fallback');
const items = [];
console.log(items ? 'array exists' : 'no array', items.length ? 'has items' : 'empty');Run with Node.js v24.22.1 as js-27.js; two runs gave identical output. Output: boolean "false" false number "0" false number "0" false bigint "0" false string "" false object "null" false undefined "undefined" false number "NaN" false object "" true object "[object Object]" true string "0" true string "false" true string " " true number "-1" true number "Infinity" true 10 0 guest null second 0 fallback first array exists empty
Key points
- Falsy values: false, 0, -0, 0n, "", null, undefined, NaN.
- Everything else is truthy, including [], {}, "0", and "false".
- && and || return operands, not booleans.
- || treats 0 and "" as missing; ?? only treats null and undefined as missing.
- Use Boolean(value) or !!value to get a real boolean.
Common mistakes
- Writing if (count) when 0 is a valid count.
- Thinking an empty array is falsy and checking it without .length.
- Treating the string "false" or "0" as falsy.
- Using || for a default when 0, false, or an empty string are meaningful values.
Follow-ups
- What does a && b || c return, and why is a ternary sometimes clearer?
- How does optional chaining combine with ??
- Why is [] == false true when [] itself is truthy?
Tips
- Memorize the eight falsy values; they are the whole answer.
- Mention the empty array and the "0" string as traps.
- Show ?? as the fix for the default-value bug with 0.