Back to Lessons

Destructuring Assignment ES6

April 5, 2026

Destructuring

Extract values from arrays/objects into variables.

Destructuring Examples

// Array destructuring
let [first, second, ...rest] = [1, 2, 3, 4, 5];
console.log(first); // 1
console.log(rest);  // [3, 4, 5]

// Object destructuring
let {name, age, city = "Unknown"} = person;

// Function parameters
function greet({name, age}) {
    return `Hello ${name}`;
}

// Nested destructuring
let {address: {street, city}} = user;

Key Points

  • Rest operator ... collects remaining elements.
  • Default values: {city = "Unknown"}.
  • Renaming: {oldName: newName}.
  • Works with function parameters.