Destructuring

Extracting Values

Lesson 84
+30 XP
Step 1 of 5

What is Destructuring?

Destructuring is a convenient way to extract values from arrays or properties from objects into separate variables.

Key Concept: Instead of accessing values one by one, destructuring lets you "unpack" them all at once!

Before Destructuring:

// The old way - repetitive!
let user = { name: 'Alice', age: 25, city: 'NYC' };

let name = user.name;
let age = user.age;
let city = user.city;

With Destructuring:

// The new way - clean and concise!
let user = { name: 'Alice', age: 25, city: 'NYC' };

let { name, age, city } = user;

console.log(name);  // 'Alice'
console.log(age);   // 25
console.log(city);  // 'NYC'

Why Use Destructuring?

  • Write less code
  • More readable and cleaner
  • Extract only the values you need
  • Works with arrays and objects