Sub topic 3
Supplying Default Values
This chapter assumes you already know basic array and object destructuring from the previous two chapters. It also assumes you are comfortable with let, const, and calling functions.
When a pattern asks for a value that isn't there, the variable gets undefined. A default value is a fallback you write in the pattern with =. JavaScript uses it in place of undefined.
Defaults in Array Patterns
Put = value after any element in an array pattern:
const [a = 1, b = 2, c = 3] = [10, undefined];
console.log(a); // 10 (value present, default ignored)
console.log(b); // 2 (value was undefined)
console.log(c); // 3 (no element at index 2)A missing element and an element that holds undefined both trigger the default. Array holes behave the same way, so const [m = 'x'] = [,] gives 'x'.
Defaults in Object Patterns
Object patterns use the same syntax after the property name:
const config = { size: 'lg' };
const { theme = 'light', size = 'md' } = config;
console.log(theme); // 'light' (property missing)
console.log(size); // 'lg' (property present)This is the most common use of defaults. It lets you read a partial configuration object without a chain of if checks or || fallbacks.
The Rule: Only `undefined` Triggers a Default
The engine checks each value against undefined with strict equality. Any other value is kept as it is, including null, 0, '', and false.
const [n = 1, z = 1, e = 'none'] = [null, 0, ''];
console.log(n, z, e); // null 0 ''The || operator works differently because it replaces every falsy value. A later chapter, "When Defaults Don't Apply and Destructuring Throws", looks at null in more detail.
Defaults Are Evaluated Lazily
A default can be any expression, including a function call. It is evaluated only when it is needed:
function fallback() {
console.log('fallback called');
return 0;
}
const [x = fallback()] = [5]; // nothing logged
const [y = fallback()] = []; // logs 'fallback called'
console.log(x, y); // 5 0So an expensive default costs nothing when a real value is supplied.
Defaults Can Use Earlier Variables
Patterns are processed left to right, and each default can refer to variables bound earlier in the same pattern:
const [w = 10, h = w * 2] = [];
console.log(w, h); // 10 20
const { min = 0, max = min + 100 } = { min: 50 };
console.log(min, max); // 50 150Referring to a variable that comes later fails. That variable is still in its temporal dead zone, the period before a let or const binding is initialized:
try {
const [p = q, q = 1] = [];
} catch (err) {
console.log(err instanceof ReferenceError); // true
}Common Errors
| Mistake | What happens | Fix | Column | Column |
|---|---|---|---|---|
| Expecting a default to replace `null` | Variable stays `null` | Use `??` after destructuring | ||
| Default refers to a later variable | `ReferenceError` | Reorder the pattern | ||
| Using ` | ` instead of a default | `0`, `''`, `false` get replaced | Use a pattern default |
Exercise
What does this log? const { count = 10, label = 'n/a', ratio = count / 2 } = { count: 0, label: undefined }; console.log(count, label, ratio);
Show answer
It logs 0 'n/a' 0. count is 0, which is not undefined, so its default is ignored. label is undefined, so it gets 'n/a'. ratio is missing, so its default runs and uses the already-bound count (0), giving 0 / 2 = 0.
Exercise
Write one array destructuring statement that takes [undefined, 4] and produces start = 1 and end = 4, and also sets step to end - start when no third element exists.
Show answer
const [start = 1, end = 10, step = end - start] = [undefined, 4]; This gives start = 1, end = 4, and step = 3.