Destructuring & SpreadΒΆ
1. DestructuringΒΆ
Destructuring assignment is a special syntax that allows us to "unpack" arrays or objects into a bunch of variables.
Array DestructuringΒΆ
const fruits = ["Banana", "Orange", "Apple"];
let [fruit1, fruit2] = fruits;
console.log(fruit1); // Banana
console.log(fruit2); // Orange
Object DestructuringΒΆ
const person = {
firstName: "John",
lastName: "Doe",
age: 50
};
let {firstName, lastName} = person;
console.log(firstName); // John
2. Spread Operator (...)ΒΆ
The spread operator expands an iterable (like an array) into more elements.
Combining ArraysΒΆ
Copying ObjectsΒΆ
Rest Parameter
The same ... syntax is used for Rest Parameters in functions, which collects all remaining arguments into an array: function sum(...args) { ... }.