JavaScript Array.map() and arrow functions

In newer versions of JavaScript (ECMAScript 5 +) we have the wonderful Array.map() method, and in even newer versions (ECMAScript 6+ aka ECMAScript 2015) we have the new arrow function expressions.

This allows for concise and powerful code like the following:

// an Array of integers
var array1 = [1, 4, 9, 16];

// create a new Array from the original Array using a function
const map1 = array1.map(x => x * 2);

// display; expected output: Array [2, 8, 18, 32]
console.log(map1);

Leave a Reply