Unpack arrays and objects cleanly and use spread and rest operators for flexible non-mutating data handling.
Learn how variables act as labeled containers for data using const, let, and var to manage lexical scope and reassignment safely in modern applications.
ES6 introduced Destructuring for unpacking values from arrays or objects directly into distinct variables, and the versatile Spread/Rest operator (...).
Destructuring eliminates verbose const name = user.name repetition, while Spread allows clean, non-mutating shallow copying and object merging.
// Object Destructuring
const user = { id: 101, name: "Maria", role: "Admin" };
const { name, role } = user;
console.log(name, role); // "Maria" "Admin"
// Array Destructuring & Rest Operator
const colors = ["Red", "Green", "Blue", "Yellow"];
const [primary, secondary, ...otherColors] = colors;
console.log(primary); // "Red"
console.log(otherColors); // ["Blue", "Yellow"]
Use spread ... to copy or merge objects and arrays without mutating the original source:
const defaultSettings = { theme: "dark", notifications: true };
const userSettings = { theme: "light" };
// Merge objects (User settings override defaults)
const finalConfig = { ...defaultSettings, ...userSettings };
console.log(finalConfig); // { theme: "light", notifications: true }
undefined or null (e.g. const { name } = undefined) throws an uncaught TypeError. Always supply default fallback objects!{ ...obj }) creates a shallow copy. If the object contains nested objects, nested properties still share references in memory!Use destructuring for clean function arguments. Next, let's understand the difference between Shallow Copy and Deep Copy!