Sub topic 13
When Defaults Don't Apply and Destructuring Throws
This chapter assumes you already know object and array destructuring, default values in patterns, nested patterns, and parameter destructuring from earlier chapters. It covers two rules behind many destructuring bugs: a default applies only when the value is undefined, and destructuring null or undefined throws a TypeError.
Defaults Trigger Only on `undefined`
A default is not a fallback for "falsy" or "empty" values. JavaScript checks whether the value is strictly undefined, and only then uses the default. A missing property and an explicit undefined both count. Every other value, including null, 0, '', and false, is kept as it is.
const { a = 1, b = 2, c = 3, d = 4, e = 5 } = { a: 0, b: '', c: false, d: null };
console.log(a, b, c, d, e); // 0 '' false null 5
const { v = 5 } = { v: undefined };
console.log(v); // 5This is a problem with JSON from APIs, which often uses null for "no value". If you want null replaced as well, use the nullish coalescing operator ?? after destructuring:
const response = { limit: null };
const { limit: rawLimit } = response;
const limit = rawLimit ?? 20;
console.log(limit); // 20Default expressions are also evaluated lazily, so they run only when they are needed:
function expensive() {
console.log('computing default');
return 1;
}
const [n = expensive()] = [5];
console.log(n); // 5, and 'computing default' is never printedWhen Destructuring Throws
Destructuring reads properties from the source. null and undefined have no properties, so they cause an error. Other primitives are temporarily wrapped in objects, so they work:
const { length } = 'abc';
console.log(length); // 3
try {
const { length: len } = null;
} catch (err) {
console.log(err instanceof TypeError); // true
}The `= {}` guard does not cover `null`
The = {} default on a nested pattern protects you only when the intermediate object is missing. It does not help when the intermediate object is null:
const order = { shipping: null };
try {
const { shipping: { city } = {} } = order;
} catch (err) {
console.log(err.message); // TypeError: cannot destructure 'city' of null
}
const { shipping } = order;
const { city } = shipping ?? {};
console.log(city); // undefinedParameters without a whole-object default
If a parameter pattern has no default for the whole object, calling the function with no arguments destructures undefined:
function connect({ port = 80 }) {
return port;
}
try {
connect();
} catch (err) {
console.log(err instanceof TypeError); // true
}
function safeConnect({ port = 80 } = {}) {
return port;
}
console.log(safeConnect()); // 80safeConnect(null) still throws, because null is not undefined and so does not trigger the = {} default.
| Source value | Default used? | Throws? |
|---|---|---|
| `undefined` (as a property value) | Yes | No |
| `null`, `0`, `''`, `false` (as a property value) | No | No |
| `undefined` (as the whole source) | Only if the pattern has its own default | Yes, without that default |
| `null` (as the whole source) | No | Yes |
Exercise
What does this log, and why? const { size = 'M', color = 'red' } = { size: null, color: undefined }; console.log(size, color);
Show answer
It logs null 'red'. The default for color applies because its value is undefined. The value of size is null, and null does not trigger a default.
Exercise
The function function show({ title = 'Untitled' } = {}) { return title; } works for show() but throws for show(null). Rewrite it so both calls return 'Untitled'.
Show answer
Destructure after normalizing the argument: function show(opts) { const { title = 'Untitled' } = opts ?? {}; return title; }. The ?? operator turns both null and undefined into {}, so no TypeError occurs and the default for title applies.