Sub topic 9
Nullish Coalescing with ??
This chapter assumes you know the falsy values, how || returns one of its operands, and why || replaces valid values like 0 and "" when you use it for defaults. Here you will learn the operator built to fix that problem.
What ?? Checks
The nullish coalescing operator ?? returns its right-hand operand only when the left-hand operand is null or undefined. Together, those two values are called nullish. Any other value, including every other falsy value, is returned unchanged.
The difference from || comes down to the question each operator asks:
| Operator | Question it asks about the left side | Falls back for | Column | Column |
|---|---|---|---|---|
| `\ | \ | ` | Is it falsy? | `false`, `0`, `-0`, `0n`, `""`, `null`, `undefined`, `NaN` |
| `??` | Is it nullish? | `null`, `undefined` |
Side-by-Side with ||
const settings = { volume: 0, name: "" };
console.log(settings.volume || 50); // 50 (0 is lost)
console.log(settings.volume ?? 50); // 0
console.log(settings.name ?? "Anonymous"); // ""
console.log(settings.theme ?? "light"); // "light"The user explicitly set volume to 0. With ||, that choice is silently replaced by 50. With ??, the 0 is kept. The theme property does not exist, so reading it gives undefined, and ?? supplies "light".
?? Also Short-Circuits
Like || and &&, ?? evaluates its right side only when it needs to. A costly or side-effecting default runs only when the left side is nullish:
function loadDefault() {
console.log("loading default");
return 42;
}
const a = 7 ?? loadDefault(); // nothing logged
const b = undefined ?? loadDefault(); // logs "loading default"
console.log(a, b); // 7 42Chaining Several Fallbacks
?? is left-associative, so you can chain it to take the first value that is not nullish:
const cliArg = undefined;
const envVar = null;
const configValue = 0;
const port = cliArg ?? envVar ?? configValue ?? 8080;
console.log(port); // 0The result is 0, because configValue is the first operand that is not nullish. A chain of || would have skipped past it to 8080.
Choosing Between || and ??
Use ?? when 0, "", false, or NaN are legitimate values that should survive. Typical cases are counts, prices, toggles, and text the user may deliberately leave empty.
Use || when every falsy value should count as "missing." One example is a display name, where an empty string should also show a placeholder.
Common Errors
- Expecting
??to catchNaNor"".NaN ?? 1isNaN, and"" ?? "x"is"". If those values should trigger a fallback, check for them explicitly. - Mixing
??with||or&&without parentheses. That is aSyntaxError. A later chapter covers the precedence rules. - Assuming
??treatsfalseas missing.false ?? truereturnsfalse.
Exercise
What does each line log? console.log(0 ?? 5); console.log(null ?? "" ?? "x"); console.log(false || null ?? "y");
Show answer
The first line logs 0, because 0 is not nullish. The second line logs "", because null is nullish but "" is not, so the chain stops at "". The third line never runs: the whole script fails to parse with a SyntaxError, because ?? cannot be mixed directly with || unless parentheses are added.
Exercise
A function receives options.retries, which a caller may set to 0 to disable retries. Write one line that sets retries to 3 only when the caller did not provide a value.
Show answer
const retries = options.retries ?? 3; Using || here would turn an explicit 0 into 3 and quietly re-enable retries.