The map function create a new array from an existing array.

Example Example

First let’s check the example of map function

Code Snippet Code Snippet

Suppose we have a below array.

const numbers = [1, 2, 3];
// [1, 2, 3]

Top ↑

Manipulate values Manipulate values

Manipulate our existing array values by adding +1 in each value as:

const newNumbers = number => number + 1 );

Here, we have used map method in which we perform the addition of +1 in each array value.

And a new array created and assigned to variable newNumbers.

The statement number => number + 1 is a arrow function. Read more about arrow functions.

Top ↑

Output Output

Final we see the output as below:

console.log( newNumbers );
// [2, 3, 4]
%d bloggers like this: