javascript destructive assignment code example

Example 1: javascript deconstruct object

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'

Example 2: how destructuring works in javascript

//destructuring in javascript
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'

Example 3: destructure to object

({x: oof.x, y: oof.y} = foo);