SyntaxStudy
Sign Up
JavaScript Default Values in Destructuring
JavaScript Intermediate 3 min read

Default Values in Destructuring

Destructuring Defaults

Defaults apply when the property is undefined — not when it is null, 0, or "".

Example
const { a = 1, b = 2 } = { a: undefined, b: null };
// a = 1 (undefined triggers default), b = null (null does not)
const [x = 10, y = 20] = [5];
// x = 5, y = 20
// Combining rename, default, and nesting:
const { config: { theme = "light", lang = "en" } = {} } = settings;
Pro Tip

null does not trigger defaults — only undefined does. Plan accordingly.