JavaScript Array.prototype.map() Method
The map() method creates a new array by calling a function on every element of an array and collecting the results. The original array is not changed.
Description
The map() method of Array instances creates a new array filled with the results of calling a function on every element. It is the usual way to transform a list, for example to turn an array of numbers into their squares, or an array of user objects into an array of names.
The new array always has the same length as the original, and the original array is left unchanged. The callback should return a value for every element; if it doesn't, the corresponding element is undefined. Empty slots in sparse arrays are skipped and stay empty.
Use map() when you want the resulting array. If you only want to run code for each element, use forEach() or a for...of loop instead; to keep only some elements, use filter(). A common mistake is passing a function that takes extra parameters, such as ['1', '2', '3'].map(parseInt), which gives [1, NaN, NaN] because parseInt() receives the index as its radix.
Syntax
array.map(callbackFn)
array.map(callbackFn, thisArg)Parameters
- callbackFnA function called once for each element. Its return value becomes the element in the new array. It receives:
element: the current element.index: its index.array: the arraymap()was called on. - thisArgOptional. A value to use as
thisinsidecallbackFn.
Return Value
A new array, the same length as the original, where each element is the result of the callback.
Example
Browser Support
The following information will show you the current browser support for the Array.prototype.map() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





