Array.prototype.map
- Version
- es2025
- Review
- Draft
Summary#
Use Array.prototype.map to transform each present element into a value in a new array. Each returned value keeps the index of the element that produced it.
Syntax#
const transformed = array.map((value, index, source) => transform(value));
Example#
const names = ["ada", "grace"];
const upper = names.map((name) => name.toUpperCase());
console.log(upper.join(", "));
console.log(names.join(", "));
Expected output#
ADA, GRACE
ada, grace
Common mistakes#
With a callback that uses braces, write return explicitly. For side effects without a result array, use for...of or forEach. The array is new but any objects returned as elements can still be shared.
Version applicability#
Array.prototype.map is available from ECMAScript 5 onward. The syntax and example use ES2015 const and arrow functions.
Sources#
- Attribution
- DevLookup contributors
- License
- CC-BY-4.0