How to use arrow function to calculate squares of array in JavaScript ES6

1 Answer

0 votes
const arr = [1, 2, 3, 4, 5];

var squaresES5 = arr.map(function(x) { return x * x });
console.log(squaresES5);

const squaresES6 = arr.map(x => x * x);
console.log(squaresES6);


     
/*
run:
   
[ 1, 4, 9, 16, 25 ]
[ 1, 4, 9, 16, 25 ]
 
*/

 



answered Mar 8, 2020 by avibootz
...