JavaScript Array map()
Example
Return a new array with the square root of all the original values:
const numbers = [4, 9, 16, 25];
const newArr = numbers.map(Math.sqrt)
Try it Yourself »
More "Try it Yourself" examples below.
Definition and Usage
The map() method creates a new array with the results of calling a
function for every array element.
The map() method calls the provided function once for each element in an
array, in order.
map() does not execute the function for empty elements.
map() does not change the original array.
Browser Support
map() is fully supported in all modern browsers:
| Chrome | IE | Edge | Firefox | Safari | Opera |
| Yes | 9.0 | Yes | Yes | Yes | Yes |
Syntax
array.map(function(currentValue, index, arr), thisValue)
Parameter Values
| Parameter | Description | ||||||||
|---|---|---|---|---|---|---|---|---|---|
| function(currentValue, index, arr) | Required. A function to be run for each element in the array. Function arguments:
|
||||||||
| thisValue | Optional. A value to be passed to the function to be used as its "this" value. If this parameter is empty, the value "undefined" will be passed as its "this" value |
Technical Details
| Return Value: | An Array containing the results of calling the provided function for each element in the original array. |
|---|---|
| JavaScript Version: | ECMAScript 5 |
More Examples
Example
Multiply all the values in array with 10:
const numbers = [65, 44, 12, 4];
const newArr = numbers.map(myFunction)
function myFunction(num) {
return num * 10;
}
Try it Yourself »
Example
Get the full name for each person in the array:
const persons = [
{firstname : "Malcom", lastname: "Reynolds"},
{firstname : "Kaylee", lastname: "Frye"},
{firstname : "Jayne", lastname: "Cobb"}
];
persons.map(getFullName);
function getFullName(item) {
return = [item.firstname,item.lastname].join(" ");
}
Try it Yourself »

