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.
- 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:
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()
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. Ifuser.settingsis 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-instructuredClone(user)!
3. Optional Chaining (?.) & Nullish Coalescing (??)
Stop using || for numeric or boolean defaults!
||falls back on ANY falsy value (0,"",false,null,undefined). Ifretries = 0,retries || 3silently overwrites0with3!??falls back ONLY onnullorundefined, preserving0,false, and""!
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 []
| Task | Better Choice | Why |
|---|---|---|
| Dynamic keys (arbitrary IDs, object keys, frequent add/delete) | Map | Preserves insertion order, map.size in O(1), no prototype key collisions ("toString") |
Unique values & fast O(1) membership (has(x)) | Set | Eliminates O(n) arr.includes() scans; [...new Set(arr)] deduplicates in 1 line |
| Fixed JSON-serializable records | Plain {} / [] | 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:
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
What does 0 || 10 evaluate to vs 0 ?? 10 in JavaScript?
Why can { ...user } still accidentally mutate user.address.city?