javascript es6 object destructuring with different name code example
Example 1: javascript object destructuring rename
// Renaming
// ----------------------------------------------
const { bar: foo } = { bar: 123 };
bar // Uncaught ReferenceError: bar is not defined
foo // 123
// Renaming with default value
// ----------------------------------------------
const { bar: foo = 123 } = { potato: 456 };
bar // Uncaught ReferenceError: bar is not defined
foo // 123
potato // Uncaught ReferenceError: potato is not defined
Example 2: javascript object destructuring
//simple example with object------------------------------
let obj = {name: 'Max', age: 22, address: 'Delhi'};
const {name, age} = obj;
console.log(name);
//expected output: "Max"
console.log(age);
//expected output: 22
console.log(address);
//expected output: Uncaught ReferenceError: address is not defined
// simple example with array-------------------------------
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]