Sub topic 14
Safer Defaults with ??
This chapter assumes you already know how || and && return one of their operands, and which values are falsy. It also assumes you know that null and undefined count as "missing" values.
The problem with || for defaults
|| returns its right operand whenever the left operand is falsy. That works for missing values, but it also replaces valid values like 0, '', and false. A retry count of 0 or an empty label is real data, and || silently throws it away.
The nullish coalescing operator ?? returns its right operand only when the left operand is null or undefined. Those two values are called nullish. For any other left operand, including every falsy value that is not nullish, ?? returns the left operand unchanged.
Comparing || and ?? side by side
function withOr(options) {
return {
retries: options.retries || 3,
label: options.label || 'untitled',
verbose: options.verbose || true,
};
}
function withNullish(options) {
return {
retries: options.retries ?? 3,
label: options.label ?? 'untitled',
verbose: options.verbose ?? true,
};
}
const input = { retries: 0, label: '', verbose: false };
console.log(withOr(input)); // { retries: 3, label: 'untitled', verbose: true }
console.log(withNullish(input)); // { retries: 0, label: '', verbose: false }
console.log(withNullish({})); // { retries: 3, label: 'untitled', verbose: true }The withOr version overrides all three values the caller passed in. The verbose line is worse than that: options.verbose || true can never produce false.
| Left operand | `x \ | \ | 'd'` | `x ?? 'd'` |
|---|---|---|---|---|
| `undefined` | `'d'` | `'d'` | ||
| `null` | `'d'` | `'d'` | ||
| `0` | `'d'` | `0` | ||
| `''` | `'d'` | `''` | ||
| `false` | `'d'` | `false` | ||
| `NaN` | `'d'` | `NaN` |
Short-circuiting and chaining
Like ||, ?? evaluates its right operand only when it needs it. So an expensive fallback runs only when the left value is missing.
function loadDefault() {
console.log('loading default');
return 10;
}
console.log(5 ?? loadDefault()); // 5, and nothing is logged
console.log(null ?? loadDefault()); // logs 'loading default', then 10
console.log(null ?? undefined ?? 'last'); // 'last'?? groups left to right, so a chain returns the first operand that is not nullish.
Mixing with || and &&
JavaScript refuses to guess what you mean when ?? appears next to || or && without parentheses. The code a || b ?? c is a SyntaxError, and the error appears when the code is parsed, before any of it runs. Add parentheses to say which grouping you want:
const a = null;
const b = 0;
console.log((a || b) ?? 1); // 0
console.log(a || (b ?? 1)); // 0
console.log((a && b) ?? 1); // 1Exercise
What does each line log? console.log(false ?? 'x'); console.log(undefined ?? 0 ?? 'y'); console.log(('' || null) ?? 'z');
Show answer
false, because false is not nullish. 0, because undefined is skipped and 0 is the first non-nullish operand. 'z', because '' || null evaluates to null, so ?? falls back to 'z'.
Exercise
A function reads const port = config.port || 8080;. Why could this be a bug, and how would you fix it?
Show answer
If config.port is 0 (for example, to ask the system for any free port), || replaces it with 8080. Writing const port = config.port ?? 8080; keeps 0 and falls back only when port is null or undefined.