JavaScript Destructuring Defaults: Avoid Undefined and Subtle Bugs
Use default values in object and array destructuring correctly, including nested options, null behavior, and function parameters.
TechSilo
Technology writer & developer
Destructuring makes it easy to extract values from objects and arrays. Adding a default prevents many unnecessary checks—but the default is applied only when the extracted value is undefined.
Object defaults
const settings = { theme: 'dark' };
const { theme, pageSize = 20 } = settings;
console.log(theme); // dark
console.log(pageSize); // 20A missing property evaluates to undefined, so the default is used.
Defaults do not replace null
const config = { retries: null };
const { retries = 3 } = config;
console.log(retries); // nullIf both null and undefined should trigger a fallback, use nullish coalescing:
const retryCount = retries ?? 3; // 3Avoid retries || 3 when values like 0 are legitimate.
Default values for function options
function createRequest({ timeout = 5000, retries = 0 } = {}) {
return { timeout, retries };
}
console.log(createRequest()); // { timeout: 5000, retries: 0 }
console.log(createRequest({ timeout: 8000 })); // { timeout: 8000, retries: 0 }The second default, = {}, is important: without it, calling createRequest() would try to destructure undefined and throw.
Nested destructuring
const options = { ui: { compact: true } };
const {
ui: { compact = false, accent = 'blue' } = {},
} = options;
console.log(compact, accent); // true blueIf options.ui is explicitly null, the pattern above still throws. Normalize the input first if null is possible:
const ui = options.ui ?? {};
const { compact: isCompact = false } = ui;Array defaults
const [first = 'guest', second = 'viewer'] = ['Ada'];
console.log(first, second); // Ada viewerDestructuring defaults also work for skipped array positions: const [, second = 2] = [1].
Common mistakes
- Destructuring from null or undefined without a safe fallback.
- Expecting = value to replace null, false, or 0.
- Using deeply nested patterns when a readable ?. and ?? expression is easier.
- Reusing property names accidentally instead of renaming them: const { id: userId } = user.
Destructuring defaults are best for known, optional fields. They are not input validation: an unexpected string or object shape still needs explicit checking. If you're reviewing unfamiliar destructuring code, the [AI Code Explainer](/tools/ai-code-explainer) can help trace it.
Want to put this into practice?
Explore TechSilo's free developer tools for practical utilities you can use directly in your browser.
Related blog posts
Console.log Alternatives for Efficient JavaScript Debugging
Use browser breakpoints, console.table, console.trace, timing APIs, and structured logs to debug without flooding production consoles.
Read postUnderstanding CSS Specificity: How the Cascade Picks Styles
Understand CSS specificity, source order, cascade layers, :is(), :where(), and practical ways to debug overriding styles.
Read postUnderstanding JavaScript Closures: Scope, State, and Common Mistakes
Learn how JavaScript closures retain lexical scope, build private state, and cause stale values in callbacks, with copyable examples.
Read postUnderstanding Virtual DOM Diffing in React
How React compares render output, uses element identity and keys, and decides whether to preserve or reset component state.
Read postUnderstanding WebAssembly: When and How to Use Wasm
Understand WebAssembly modules, JavaScript integration, browser sandboxing, and the workloads where Wasm helps performance.
Read post