Destructuring assignment is special syntax introduced in ES6, for neatly assigning values taken directly from an object. const user = { name: 'John Doe', age: 34 }; const { name, age } = user; // name = 'John Doe', age = 34
const objA = { prop1: 'foo', prop2: { prop2a: 'bar', prop2b: 'baz', }, }; // Deconstruct nested props const { prop1, prop2: { prop2a, prop2b } } = objA; console.log(prop1); // 'foo' console.log(prop2a); // 'bar' console.log(prop2b); // 'baz'
let a, b, rest; [a, b] = [10, 20]; console.log(a); // expected output: 10 console.log(b); // expected output: 20 [a, b, ...rest] = [10, 20, 30, 40, 50]; console.log(rest); // expected output: Array [30,40,50]
({x: oof.x, y: oof.y} = foo);