I am having difficulty understanding arrow function in javascript. I looked up in MDN and found this code.
var res, elements = [
'Hydrogen',
'Helium',
'Lithium',
'Beryllium'
];
//First
res = elements.map(function(element) {
return element.length;
});
console.log(res);
//Second
res = elements.map(element => {
return element.length;
});
console.log(res);
//Third
res = elements.map(({length}) => length);
console.log(res);
I get the first and second way just fine, but the third method makes no sense to me.
First: I didn't understand what these 2 brackets ()
and {}
in ({ length })
are.
Second: The first word lenght
also makes no sense. Is it just some random variable?
Anybody willing to shed some light would be very helpful. Thanks in advance!