Object Destructuring in JavaScript

const cat= {
name: 'Cici',
realName: 'Cicialine'
};
const { name, realName } = cat;name; // => 'Cici',
realName; // => 'Cicialine'
const dog= {
name: 'Masha',
};
let { name } = dog;name; // => 'Masha'
const dogs= [
{ name: 'Masha' },
{ name: 'Max' }
];

for (const { name } of heroes) { console.log(name); // logs 'Masha', 'Max'
}
const heroes = [
{ name: 'Masha' },
{ name: 'Max' }
];
const names = heroes.map(
function({ name }) {
return name;
}
);
names; // => ['Masha', 'Max']
const dog= {
name: 'Masha',
realName: 'Maria The Hreat'
};

const { enemies = ['Zelda'] } = dog;
enemies; // => ['Zelda']

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store