Map Function in JavaScript

The map() method creates a new array with the results of calling a provided function on every element in this array.In the physical world, a map is a way of projecting a surface (terrain) onto another (paper). Similarly, the map function “projects” the original array into the new one.

Source code :

myArray = [1,2,3,4];

myArray.map(function (element) {
  return element + 1;
});

Output :

[ 2, 3, 4, 5 ]

Algorithm :

  1. Declare an Array.
  2. Call back map function.
  3. Increment the element value.

Output :


                

Comments :