1. Deep JavaScript Mechanics & Modern ES6+

Destructuring, Spread, Immutability & Map/Set

Write clean, bug-free ES6+ state transformations: master nested destructuring, object/array spread, why {...obj} is shallow (structuredClone), ?. and ?? operators, and Map vs Set.

4 min readUpdated 2026-12-01
In This Lesson
  • Extract nested fields cleanly with destructuring defaults and variable renaming
  • Update state immutably and avoid shallow-copy mutation bugs using structuredClone()
  • Choose accurately between ?? vs ||, and Map/Set vs plain objects/arrays

Modern frontend frameworks (React, Vue, Svelte) and TypeScript apps rely heavily on immutable updates: instead of mutating an existing array or object in place, you return a brand-new updated copy. Doing that cleanly without mutating nested state requires mastering destructuring, spread/rest, structuredClone(), ?? / ?., and Map / Set.

1. Destructuring with Defaults & Renaming

Pull properties out of objects and arrays cleanly right in assignments or function parameters:

javascript
const response = {
  status: 200,
  user: { id: 42, displayName: "Maya" },
};

// Rename displayName -> userName and supply a fallback role
const {
  status,
  user: { displayName: userName, role = "student" },
} = response;

// Array destructuring + rest element
const [first, second, ...remaining] = ["TS", "JS", "CSS", "HTML"];

2. Immutability: Shallow {...obj} vs structuredClone()

Shallow Spread {...obj} vs Deep Copy structuredClone(obj)

const copy = { ...user } (Shallow)

copy.name = "Sam" (safe)

copy.prefs.theme = "dark" → mutates user.prefs too!

structuredClone(user) (Deep)

Duplicates every nested object, array, Map, Set, and Date independently.

Object spread copies top-level keys only; nested objects/arrays stay shared unless cloned with spread at that level or structuredClone().

Spread ({ ...user, name: "Alex" }) and arr.map(...) / arr.filter(...) keep your top-level updates immutable—but watch out for nested objects!

  • { ...user } only copies one level deep. If user.settings is a nested object, copy.settings === user.settings!
  • To update nested state cleanly: { ...user, settings: { ...user.settings, theme: "dark" } }.
  • To make a completely independent deep clone (supporting nested objects, arrays, Date, Map, Set): use browser/Node built-in structuredClone(user)!

3. Optional Chaining (?.) & Nullish Coalescing (??)

Stop using || for numeric or boolean defaults!

  • || falls back on ANY falsy value (0, "", false, null, undefined). If retries = 0, retries || 3 silently overwrites 0 with 3!
  • ?? falls back ONLY on null or undefined, preserving 0, false, and ""!
javascript
const config = { retries: 0, label: "" };
console.log(config.retries || 3);   // 3 (BUG!)
console.log(config.retries ?? 3);   // 0 (Correct!)
console.log(config.nested?.host ?? "localhost"); // Safe deep read!

4. When to Prefer Map and Set Over {} and []

TaskBetter ChoiceWhy
Dynamic keys (arbitrary IDs, object keys, frequent add/delete)MapPreserves insertion order, map.size in O(1), no prototype key collisions ("toString")
Unique values & fast O(1) membership (has(x))SetEliminates O(n) arr.includes() scans; [...new Set(arr)] deduplicates in 1 line
Fixed JSON-serializable recordsPlain {} / []Works directly with JSON.stringify()

Try It Yourself in the Playground

Experiment with nested destructuring, the ?? vs || fix, shallow vs structuredClone(), and Map/Set deduplication below:

JavaScriptTry It Live: Immutability, ?? vs || & Map/Set
Output

Click “Run Code” to execute this snippet right in your browser.

Mini-Challenge: Given state = { filters: { level: "Beginner" }, items: [1, 2, 3] }, create nextState immutably changing level to "Intermediate" and adding 4 to items without mutating state.

Quick Checkpoints

Check Your Understanding

What does 0 || 10 evaluate to vs 0 ?? 10 in JavaScript?

Check Your Understanding

Why can { ...user } still accidentally mutate user.address.city?