Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Array.prototype.map: transforming elements

map calls a callback for existing elements and creates a new result array with the same length.

Version: ECMAScript 2026 draft; example target: Node.js 24.22.2

Syntax

array.map(callbackFn[, thisArg])

array.map(callbackFn[, thisArg]) creates a new array whose length is captured before the first callback call. For every existing index in ascending order it calls callbackFn(value, index, array), then stores the returned value at the same index. Sparse holes are skipped and remain holes in the result.

Elements appended beyond the initial length are not visited; changing or deleting an unvisited index affects its later step. thisArg supplies this to an ordinary function but does not change the lexical this of an arrow function. The method itself does not mutate the receiver, although the callback can. A non-callable callback or a null or undefined receiver throws TypeError; callback and result-write failures propagate. The method is generic for array-like values.

Examples

const source = [2, , 4];
const seen = [];
const doubled = source.map((value, index, array) => {
  seen.push(index + ":" + array.length);
  return value * 2;
});
console.log(doubled.length, 1 in doubled, doubled.join("|"));
console.log(seen.join(","));
const growing = [1, 2];
const result = growing.map((value, index) => {
  if (index === 0) growing.push(3);
  return value + index;
});
console.log(result.join(","), growing.join(","));

Result

3 false 4||8
0:3,2:3
1,3 1,2,3

Sources