Array.prototype.map()
Creates a new array populated with the results of calling a function on every element.
Syntax
arr.map(callback, thisArg) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
callback | Function | Yes | Function called for each element. Receives (element, index, array) and returns the new value. |
thisArg | any | No | Value to use as `this` when executing the callback. |
Returns
Array — A new array with each element being the result of the callback.
Examples
const nums = [1, 2, 3];
const doubled = nums.map(n => n * 2);
console.log(doubled);
Output
[ 2, 4, 6 ]
const users = [{ name: 'Ann' }, { name: 'Bob' }];
console.log(users.map(u => u.name));
Output
[ 'Ann', 'Bob' ]
Notes
Does not mutate the original array. Always returns a new array of the same length.
Skips empty slots in sparse arrays but preserves them in the result. Use `forEach`
if you do not need a returned array.
Browser & runtime support
| Environment | Since version |
|---|---|
| chrome | 1.0 |
| firefox | 1.5 |
| safari | 3.0 |
| edge | 12 |
| node | 0.10 |