Fundamentalsbeginner

Truthy and Falsy Values

The short, memorizable list of values JavaScript treats as false in a boolean context, and everything else.

Any value used where a boolean is expected gets converted. The falsy list is short and closed; everything else is truthy.

Falsy values are like a very short guest list of six people not allowed into the 'true' party — everyone else in the world, no matter how unusual, gets in automatically.

Key Concepts

1
The eight falsy values.
    false
    0        -0        0n        (BigInt zero)
    ''       (empty string)
    null
    undefined
    NaN
2
That is the complete list. Memorising it is worth doing, because if (value) appears everywhere.
if (value)
3
Everything else is truthy, including the ones that catch people out.
    if ('0')        // runs -- a non-empty string
    if ('false')    // runs -- also a non-empty string
    if ([])         // runs -- an empty array is an object
    if ({})         // runs
    if (function(){}) // runs
4
[] being truthy while [] == false is true looks contradictory and is not: the if converts with Boolean(), and == converts both sides to numbers.
[][] == falseifBoolean()==
5
The bug this causes most often.
    function setCount(n) {
      if (!n) n = 10;      // intended: default when missing
      ...
    }
    setCount(0);           // n becomes 10 -- wrong
6
Zero is falsy, so a legitimate value is replaced. The same happens with an empty string in a form field.
7
Which is what ?? is for.
    n = n ?? 10;      // only when n is null or undefined
    n = n || 10;      // when n is any falsy value, including 0
??
8
|| falls back on every falsy value. ?? falls back only on null and undefined. For a numeric or string input, ?? is almost always what was meant.
||??nullundefined
9
Checking existence properly.
    if (value !== undefined)        exists, even if falsy
    if (value != null)              not null and not undefined
    if (Array.isArray(a) && a.length) a non-empty array
    if (Object.keys(o).length)      a non-empty object
10
if (arr) is always true, so it tests nothing about an array's contents — a common mistake when checking whether a fetch returned anything.
if (arr)
11
The habit. Use truthiness for genuine booleans and for presence checks on objects. Test explicitly when 0, '' or false are legitimate values, which for anything coming from a user or an API they usually are.
0''false
12
What the interviewer is probing.1. "Why is if (value) risky for a number?" Probing: the zero trap. Stalls: "It is fine." Moves up: zero is falsy, so a legitimate 0 takes the else branch; test value !== undefined or use nullish coalescing.
13
2. "What does ?? do that || does not?" Probing: the distinction. Stalls: "They are the same." Moves up: ?? only falls through for null and undefined, so 0 and empty string are preserved where || would replace them.
14
3. "Is an empty array truthy?" Probing: the object rule. Stalls: "No, it is empty." *Moves up:* yes — every object is truthy, which is why [] == false and if ([]) disagree.
15
4. "How do you check for an empty string safely?" Probing: precision. Stalls: "if (!s)." Moves up: s === "" when you mean empty, since !s also catches null, undefined and 0.