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

ParameterDescription
callbackFn Called for each element with (element, index, array).

Return value

A new array of the same length.

Examples

Transform every elementTry it yourself
const nums = [1, 2, 3];
console.log(nums.map((n) => n * 2), nums);

Output

[ 2, 4, 6 ] [ 1, 2, 3 ]
Objects to stringsTry it yourself
const users = [{ first: "Ada", last: "Lovelace" }, { first: "Linus", last: "Torvalds" }];
console.log(users.map((u) => `${u.first} ${u.last}`));

Output

[ 'Ada Lovelace', 'Linus Torvalds' ]
The parseInt trapTry it yourself
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, use filter() (or flatMap()).
  • ["1","2","3"].map(parseInt) gives [1, NaN, NaN] because map() passes the index as parseInt's radix. Use map(Number) or map((s) => parseInt(s, 10)).
  • Don't use map() only for side effects; use forEach() or for…of.

Version history

VersionChange
JavaScript ES5Added

In other languages