- JavaScript
- Modern JavaScript: ES Modules, Iterators, and Weak Collections
- Destructuring Assignment
Destructuring in JavaScript: Defaults & Swaps
Destructuring in JavaScript unpacks values from arrays (by position) or properties from objects (by name) into variables in one statement. Defaults apply only when the value is undefined, patterns can be nested and renamed, and a rest element collects what is left. It is common in function parameters, swaps and React props.
Key Learning Objectives
Destructure arrays by position and objects by name, including nested and renamed patterns.
Predict when a default value is used: only for undefined, never for null.
Use rest elements, swaps and destructured function parameters with a safe default.
Avoid the TypeError from destructuring null or undefined.
The Interview Problem
What do the four console.log calls print?
1const user = { name: 'Ada', role: undefined, tags: ['js', 'react'] };2const { name, role = 'dev', age = 30, tags: [first, ...others] } = user;3console.log(name, role, age, first, others.length);45let p = 1, q = 2;6[p, q] = [q, p];7console.log(p, q);89const { length } = 'hello';10console.log(length);1112function show({ id, label = 'none' } = {}) {13 return `${id}:${label}`;14}15console.log(show({ id: 7 }), show());
Predict Console Output
Select the option that matches what this code prints to the console:
Ada dev 30 js 1 / 2 1 / 5 / 7:none undefined:none
Ada undefined 30 js 1 / 2 1 / 5 / 7:none undefined:none
Ada dev 30 js 1 / 1 2 / 5 / 7:none undefined:none
Ada dev 30 js 1 / 2 1 / 5 / TypeError
V8 Engine Execution Trace
Step 1 of 7 (Line 2)Object destructuring reads each property by name. role and age are undefined, so their defaults apply. tags is matched against an array pattern: first gets 'js' and the rest element collects ['react'].
Destructuring in JavaScript Explained
How Destructuring Assigns Values
Destructuring in JavaScript writes the shape of the data on the left side of = and pulls values out of it: array patterns match by position, object patterns by property name.
- Defaults only for undefined:
role = 'dev'is used becauseroleisundefined, andage = 30becauseageis missing. Anullvalue would staynull. - Nesting, renaming and rest:
tags: [first, ...others]matches thetagsarray against an array pattern. A rest element collects the remaining items (or, in an object pattern, the remaining properties).{ tags: labels }would instead rename the variable. - Swaps and parameters:
[p, q] = [q, p]builds the right-hand array before assigning, so it swaps without a temporary variable. In a parameter list,{ id, label = 'none' } = {}lets the function run with no argument; without= {},show()throws a TypeError, as does destructuringnullanywhere.
Common Traps & Mistakes
Expecting a default to replace null; defaults only replace undefined.
Destructuring a parameter without a = {} fallback and calling the function with no argument.
Starting a line with [ or ( after a line with no semicolon, so the swap is parsed as part of the previous expression.
FAANG Follow-Up Probes
Probe #1
How does const [count, setCount] = useState(0) rely on array destructuring?
Probe #2
Write a function that takes an options object with defaults for three settings.
Probe #3
How would you pull a deeply nested value safely when a middle object can be missing?
Destructuring in JavaScript Interview Questions
Key interview inquiries, edge cases, and architectural follow-ups for Destructuring Assignment.
When does a destructuring default value apply?
Only when the value is undefined, either missing or explicitly undefined. role: undefined gets the default 'dev', but a property set to null keeps null, because null is a real value.
What is the difference between array destructuring and object destructuring?
Array destructuring matches by position and works on any iterable, such as strings, Sets and Maps. Object destructuring matches by property name, and it can read properties of primitives too: const { length } = 'hello' gives 5.
How do you swap two variables with destructuring?
[p, q] = [q, p]. The right side builds a new array from the current values first, then the pattern assigns them back, so no temporary variable is needed.
How do you rename a variable while destructuring?
Use a colon: const { tags: labels } = user creates a variable called labels. The same position can hold a nested pattern instead, as in tags: [first, ...others].
Why write = {} after a destructured parameter?
function show({ id, label = 'none' } = {}) can then be called with no argument. Without the = {} default, show() would try to destructure undefined and throw a TypeError.
What happens if you destructure null or undefined?
It throws a TypeError, for example "Cannot destructure property 'z' of 'null' as it is null." Guard with a default, such as const { z } = value ?? {}.
More JavaScript interview questions for freshers
