-- Main.shssdhakchina - 26 Jan 2022
Destructuring Assignment in Javascript
Destructuring Assignment is a JavaScript expression that allows to unpack values from arrays, or properties from objects, into distinct variables data can be extracted from arrays, objects, nested objects and assigning to variables. In Destructuring Assignment on the left-hand side defined that which value should be unpacked from the sourced variable.
Syntax:
Array destructuring:
var x, y;
[x, y] = [10, 20];
console.log(x); // 10
console.log(y); // 20
or
[x, y, ...restof] = [10, 20, 30, 40, 50];
console.log(x); // 10
console.log(y); // 20
console.log(restof); // [30, 40, 50]
Object destructuring:
({ x, y} = { x: 10, y: 20 });
console.log(x); // 10
console.log(y); // 20
or
({x, y, ...restof} = {x: 10, y: 20, m: 30, n: 40});
console.log(x); // 10
console.log(y); // 20
console.log(restof); // {m: 30, n: 40}