JavaScript Array map() method

The JavaScript array map() method calls the specified function for every array element and returns the new array. This method doesn't change the original array.

Syntax

The map() method is represented by the following syntax:

Parameter

callback - It represents the function that produces the new array.

currentvalue - The current element of array.

index - It is optional. The index of current element.

arr - It is optional. The array on which map() method operated.

thisArg - It is optional. The value to use as this while executing callback.

Return

A new array whose each element generate from the result of a callback function.

JavaScript Array map() method example

Let's see some examples of map() method.

Example 1

Here, map() method returns the round of given elements to the nearest integer.

Test it Now

Output:

2,4,5

Example 2

Here, map() method returns every element of given array by multiplying it by 3.

Test it Now

Output:

6,12,18





Latest Courses