destructuring of array in javascript code example
Example 1: array destructuring
//Without destructuring
const myArray = ["a", "b", "c"];
const x = myArray[0];
const y = myArray[1];
console.log(x, y); // "a" "b"
//With destructuring
const myArray = ["a", "b", "c"];
const [x, y] = myArray; // That's it !
console.log(x, y); // "a" "b"
Example 2: object destructuring example
const hero = {
name: 'Batman',
realName: 'Bruce Wayne',
address: {
city: 'Gotham'
}
};
// Object destructuring:
const { realName, address: { city } } = hero;
city; // => 'Gotham'