JavaScript (ES2026) reference · Array Methods
JavaScript Array map() Method
Return a new array with the results of calling a function on every element. The original array is unchanged.
JavaScript (ES2026) Since ES5
Syntax
array.map(callbackFn, thisArg)
Parameters
| Parameter | Description |
|---|---|
callbackFn |
Called for each element with (element, index, array). |
Return value
A new array of the same length.
Examples
const nums = [1, 2, 3];
console.log(nums.map((n) => n * 2), nums);
Output
[ 2, 4, 6 ] [ 1, 2, 3 ]
const users = [{ first: "Ada", last: "Lovelace" }, { first: "Linus", last: "Torvalds" }];
console.log(users.map((u) => `${u.first} ${u.last}`));
Output
[ 'Ada Lovelace', 'Linus Torvalds' ]
console.log(["1", "2", "3"].map(parseInt)); // surprise!
console.log(["1", "2", "3"].map(Number));
Output
[ 1, NaN, NaN ] [ 1, 2, 3 ]
Common mistakes
map()always returns a new array of the same length. To remove items, usefilter()(orflatMap()).["1","2","3"].map(parseInt)gives[1, NaN, NaN]becausemap()passes the index asparseInt's radix. Usemap(Number)ormap((s) => parseInt(s, 10)).- Don't use
map()only for side effects; useforEach()orfor…of.
Version history
| Version | Change |
|---|---|
| JavaScript ES5 | Added |
In other languages
map()· Python
Related
array.filter()· Return a new array with only the elements that pass a test function. The original array is not changed.array.forEach()· Call a function once for each element. It returns undefined, so use map() when you need a new array.array.flatMap()· Map each element with a function, then flatten the result one level. Useful when one element becomes several.array.reduce()· Reduce the array to a single value by running a reducer function on each element, carrying an accumulator along.- JavaScript Arrays · tutorial
- JavaScript Array Methods · tutorial