The following code is example code from an answer to a question on arrow functions.
var a = [
"We're up all night 'til the sun",
"We're up all night to get some",
"We're up all night for good fun",
"We're up all night to get lucky"
];
// These two assignments are equivalent:
// Old-school:
var a2 = a.map(function(s){ return s.length });
// ECMAscript 6 using arrow functions
var a3 = a.map( s => s.length );
// both a2 and a3 will be equal to [31, 30, 31, 31]
I'm new to javascript, so I'm wondering what's the point of defining a function this way at all? It seems to me it would be much easier to just pass in the s.length
value itself. Even with more complex operations, it seems like it would make more sense to either save the output of those operations into a variable and pass that variable into the function a.map
or just define the operations inside of the parentheses following a.map
.