Destructuring assignment simplifies the process of assigning array values and object properties to variables.
// Create an Object const person = { firstName: “John”, lastName: “Doe”, age: 50, eyeColor: “blue” }; // Destructuring Assignment let { firstName, age } = person; |
Note: When destructuring an object, the variable names must match the corresponding object keys. The order of the keys does not matter. |
Destructuring assignment allows for easy extraction of array values and object properties into variables.
// Create an Array const fruits = [“Banana”, “Orange”, “Apple”, “Mango”]; // Destructuring Assignment let [fruit1, fruit2] = fruits; |
The … operator spreads an iterable (such as an array) into individual elements.
const q1 = [“Jan”, “Feb”, “Mar”]; const q2 = [“Apr”, “May”, “Jun”]; const q3 = [“Jul”, “Aug”, “Sep”]; const q4 = [“Oct”, “Nov”, “May”]; const year = [...q1, ...q2, ...q3, ...q4]; |
The … operator can be used to spread an iterable into individual arguments in a function call.
const numbers = [23,55,21,87,56]; let maxValue = Math.max(...numbers); |