Destructuring allows you to extract values from arrays or objects into distinct variables.
Array Destructuring:
const numbers = [1, 2, 3];
const [a, b, c] = numbers;
console.log(a, b, c); // 1 2 3
Object Destructuring:
const person = {
firstName: "Bob",
lastName: "Smith",
age: 30,
};
const { firstName, age } = person;
console.log(firstName, age); // "Bob", 30
You can also rename variables:
const { firstName: nameAlias, age: personAge } = person;
console.log(nameAlias, personAge); // "Bob", 30