Destructuring in JavaScript: Defaults & Swaps

Easy100% Free~12 mins#destructuring#default-values#rest#es6#function-parameters
Interview Answer (30-Second Summary)

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);
4
5let p = 1, q = 2;
6[p, q] = [q, p];
7console.log(p, q);
8
9const { length } = 'hello';
10console.log(length);
11
12function show({ id, label = 'none' } = {}) {
13 return `${id}:${label}`;
14}
15console.log(show({ id: 7 }), show());
Predict Console Output
Interactive Challenge

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'].

Call Stack (Top = Active)
Global
Lexical Scope / Bindings
name:'Ada'
role:'dev' (default, value was undefined)
age:30 (default, missing)
first:'js'
others:['react']
Console Stream
> [empty]

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.

  1. Defaults only for undefined: role = 'dev' is used because role is undefined, and age = 30 because age is missing. A null value would stay null.
  2. Nesting, renaming and rest: tags: [first, ...others] matches the tags array 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.
  3. 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 destructuring null anywhere.
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.

Q1.
What is destructuring in JavaScript?

A syntax for unpacking values into variables: const [a, b] = arr takes elements by position, and const { name, age } = obj takes properties by name. It was added in ES2015 (ES6).

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.

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.

[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.

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].

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.

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