← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

Destructuring & Spread (JS)

javascript ES2015 Beginner

Also Known As

JS destructuring object destructuring array destructuring JS

TL;DR

Destructuring extracts values from arrays/objects into variables; spread (...) expands iterables — both core to modern JavaScript style.

Explanation

Array destructuring: const [first, , third] = arr — skips can use empty slots; rest elements: const [head, ...tail] = arr. Object destructuring: const { name, age = 18 } = person — default values for absent keys; rename: const { name: userName } = person; nested: const { address: { city } } = user. Spread in arrays: const merged = [...arr1, ...arr2]; in objects (ES2018): const copy = { ...obj, newProp: 1 } — shallow clone with override. Function parameters: function process({ id, name }) {} — named parameter pattern. Rest in objects: const { id, ...rest } = record — omit known keys. Compared to PHP 8.1 array unpacking and named arguments, JS destructuring is more flexible and widely used.

Diagram

flowchart LR
    subgraph Object_Destructuring
        OBJ[const user = name email role]
        OBJ -->|destructure| VARS[const name email = user]
        VARS -->|rename| RENAME[const n: name = user]
        VARS -->|default| DEFAULT[const role = admin = user]
    end
    subgraph Array_Destructuring
        ARR[const arr = 1 2 3]
        ARR -->|destructure| AVALS[const a b c = arr]
        ARR -->|skip| SKIP[const a _ c = arr]
        ARR -->|rest| REST[const first ...others = arr]
    end
    subgraph Function_Params
        FN[function greet name greeting = Hi]
        FN -->|called with| CALL[greet name: Alice]
    end
style OBJ fill:#6e40c9,color:#fff
style ARR fill:#1f6feb,color:#fff
style FN fill:#238636,color:#fff

Common Misconception

Destructuring creates a deep copy of the destructured value. Destructuring only unpacks references — destructuring an object gives you references to the same nested objects. Mutating a destructured object property mutates the original.

Why It Matters

Destructuring extracts values from arrays and objects into named variables — replacing error-prone index access with self-documenting, concise extraction that reduces temporary variable noise.

Common Mistakes

  • Not using default values in destructuring when a property may be missing: const { name = 'Guest' } = user.
  • Nested destructuring that becomes hard to read — extract intermediate variables for deeply nested structures.
  • Array destructuring for associative data that would be clearer as object destructuring.
  • Not using rest in destructuring: const { id, ...rest } = user to separate one property from the rest.

Code Examples

✗ Vulnerable
// Manual property access — verbose:
const name = user.name;
const email = user.email;
const city = user.address.city;

// Destructuring:
const { name, email, address: { city } } = user;

// With rename and default:
const { name: fullName, role = 'user' } = user;
✓ Fixed
// Swap without temp variable
[a, b] = [b, a];

// Object rest — strip id from record
const { id, ...payload } = record;

// Default + rename
const { timeout: ms = 3000, retries = 3 } = options;

Added 15 Mar 2026
Edited 22 Mar 2026
Views 48
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 2 pings S 1 ping M 1 ping T 0 pings W 1 ping T 2 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T
No pings yesterday
Amazonbot 12 ChatGPT 8 Google 6 Perplexity 6 Unknown AI 4 Ahrefs 2 Majestic 2 SEMrush 2
crawler 38 crawler_json 1 pre-tracking 3
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Low
⚡ Quick Fix
Use destructuring to extract values clearly: const {name, age = 0} = user and const [first, ...rest] = items — default values prevent undefined, rest collects remaining items
📦 Applies To
javascript ES2015 web cli
🔗 Prerequisites
🔍 Detection Hints
const name = user.name; const age = user.age; that could be destructured; accessing array indices directly when destructuring would clarify intent
Auto-detectable: ✓ Yes eslint typescript
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: High ✓ Auto-fixable Fix: Low Context: Function

✓ schema.org compliant