Example 1: destructuring arrays with rest operator
const [q, r, ...callThisAnythingYouWant] = [1, 2, 3, 4, 5, 6, 7, 8];
console.log(q, r);
console.log(callThisAnythingYouWant);
Example 2: Destructuring Assignment
const HIGH_TEMPERATURES = {
yesterday: 75,
today: 77,
tomorrow: 80
};
const {today, tomorrow} = HIGH_TEMPERATURES;
const today = HIGH_TEMPERATURES.today;
const tomorrow = HIGH_TEMPERATURES.tomorrow;
console.log(today);
console.log(tomorrow);
console.log(yesterday);
Example 3: destructuring objects
({ a, b } = { a: 10, b: 20 });
console.log(a);
console.log(b);
({a, b, ...rest} = {a: 10, b: 20, c: 30, d: 40});
console.log(a);
console.log(b);
console.log(rest);
Example 4: array destructuring
const myArray = ["a", "b", "c"];
const x = myArray[0];
const y = myArray[1];
console.log(x, y);
const myArray = ["a", "b", "c"];
const [x, y] = myArray;
console.log(x, y);
Example 5: object destructuring
let a, b, rest;
[a, b] = [10, 20];
console.log(a);
console.log(b);
[a, b, ...rest] = [10, 20, 30, 40, 50];
console.log(rest);
Example 6: javascript destructing
const x = [1, 2, 3, 4, 5]
const [a, b] = x
console.log(a)
console.log(b)