In JavaScript
, the map()
method creates a new array from an existing array. It calls a provided callback function on every array element. map()
does not mutate the original array.
The map()
method accepts the following two parameters.
function
: It is a required parameter called on every element of the array. It accepts the following three parameters.
value
: A required parameter. It holds the value of the current element of the array.
index
: This is an optional parameter, as it holds the index of the current element of the array.
array
: This is an optional parameter. It holds the actual array on which map()
is called.
this
: It is an optional parameter and specifies the value of this pointer while executing the callback function.
array.map(function(value, index, array), this);
The map()
method returns a new array populated with the result of the callback function on each element of the existing array.
The following example illustrates the use of map()
method.
const array = [4, 5, 7, 15, 1, 2, 6, 8, 1, 10];const newArray = array.map(ele => ele * 3);console.log(newArray);