JavaScript theorytheory 0/50 · 0%
Fundamentals · easy

8. Truthiness and nullish handling

Falsy values, `??` and `?.`

The falsy values are `false`, `0`, `-0`, `0n`, `""`, `null`, `undefined` and `NaN`; everything else is truthy. `||` falls back on any falsy value, while `??` falls back only on `null`/`undefined` — which matters when `0` is a legitimate value.

const gas = input ?? 21000;      // keeps 0
const label = name || "unknown"; // replaces ""
const rpc = cfg?.network?.rpc;   // undefined instead of a crash

Check your understanding

  1. 1. Which operator keeps a valid `0`?

  2. 2. Is `"0"` truthy?

  3. 3. What does `a?.b` do when `a` is undefined?