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: destructure array javascript
var [first, second, ...rest] = ["Mercury", "Earth", ...planets, "Saturn"];
Example 3: 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 4: javascript function destructuring
function f() {
return [1, 2];
}
let a, b;
[a, b] = f();
console.log(a);
console.log(b);
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: mdn destructuring
let a, b;
[a, b] = [1, 2];
console.log(a);
console.log(b);