How does array and object destructuring work
Destructuring pattern-matches a value's shape: arrays by index, objects by key. Supports defaults (`= 0`), renaming (`{a: b}`), rest (`...rest`), nested patterns, and works in parameters. Under the hood, it desugars to indexed/property access — array destructuring uses the iterator protocol.
Destructuring is pattern-matching on the shape of a value to bind names — concise, but it has subtle rules.
Array destructuring — by position (iterator)
const [a, b, c] = [1, 2, 3]; // a=1, b=2, c=3
const [, , third] = arr; // skip
const [x, ...rest] = arr; // rest is a real array
const [p = 0, q = 0] = [1]; // defaults: p=1, q=0Array destructuring uses the iterator protocol — it works on anything iterable, not just arrays:
const [first] = new Set([10, 20]); // 10
const [a, b] = "hi"; // 'h', 'i'Object destructuring — by key
const { name, age } = user;
const { name: n } = user; // rename
const { name = "anon" } = user; // default if undefined
const { a: { b } } = obj; // nested
const { name, ...rest } = user; // rest objectDefaults fire only on undefined
null does NOT trigger the default — a common gotcha:
const { x = 1 } = { x: null }; // x = null, NOT 1
const { x = 1 } = {}; // x = 1In function parameters
function fetchUser({ id, name = "anon" } = {}) { /* ... */ }The trailing = {} lets the function be called with no arg without throwing on the destructure.
Computed and dynamic keys
const key = "name";
const { [key]: value } = user;Subtle gotchas
- Destructuring
null/undefinedthrows —const { x } = nullis a TypeError. Provide a default:const { x } = obj ?? {}. - Statement vs expression —
{ a } = objat statement start is a block; wrap:({ a } = obj). - Order matters when defaults reference earlier bindings:
const { a = 1, b = a + 1 } = {}.
Interview framing
"Destructuring is pattern-matching on a value's shape — arrays by index via the iterator protocol, objects by key. It supports defaults (only on undefined), renaming, rest, and nested patterns. The common gotchas: destructuring null throws, defaults don't trigger on null, and you need parens around an object destructure used as a statement."
Follow-up questions
- •Why does the default not trigger on null?
- •How does array destructuring work on a Set or a string?
- •How would you safely destructure from a possibly-null value?
Common mistakes
- •Expecting defaults to apply to null.
- •Forgetting parens around statement-level object destructuring.
- •Destructuring a possibly-null value without a fallback.
Performance considerations
- •Negligible — desugars to index/property access. Array destructuring uses the iterator protocol, which is slightly more involved than direct indexing but rarely matters.
Edge cases
- •Destructuring from `undefined`/`null` (throws).
- •Empty arrays / missing keys.
- •Renaming + default combined: `{ a: b = 1 }`.
Real-world examples
- •Destructuring props in React components.
- •Pulling fields from API responses.
- •Options objects with defaults in library APIs.