To map an array of elements in JavaScript, use the array.map() function. The array.map() is a built-in JavaScript pure function that creates a new array populated with the results of calling a provided function on every item in the calling array.
The map() function creates a new array by calling a function for every array element. The map() does not execute the function of every item. The map() does not change an original array, and that’s why it is a pure function.
Syntax
array.map(function(current value, index, array), this value)
Arguments
- function(): It is a function to be run for each array element.
- current value: It is a value of the current element.
- index: It is an index of the current element.
- array: The array for the current element.
- this value: A value passed to the function to be used as its value.
Return value
It is an array resulting from a function for each array element.
Example
const num = [10, 20, 30, 40, 50]
const newarr = num.map(myfun)
function myfun(n) {
return n * 10;
}
console.log(newarr);
Output
[ 100, 200, 300, 400, 500 ]
In this example, you can see that we are multiplying each element by 10 using the array.map() function. The map() function calls a provided CALLBACK FUNCTION once for each item in an array, in order, and constructs a new array from the results.
That’s it for this tutorial.
See also
How to flatten Array of Objects in JavaScript
How to read an array of objects in JavaScript
How to combine arrays in JavaScript

Krunal Lathiya is a Software Engineer with over eight years of experience. He has developed a strong foundation in computer science principles and a passion for problem-solving. Krunal has experience with various programming languages and technologies, including PHP, Python, and expert in JavaScript. He is comfortable working in front-end and back-end development.