Array.prototype.map()
map(callbackFn, thisArg?) Returns:
Array · Added in vES5 · Updated March 13, 2026 · Array Methods array map functional transformation
map() creates a new array populated with the results of calling a provided function on every element in the calling array. It transforms data by applying a function to each item, making it one of the most commonly used array methods in JavaScript.
Syntax
map(callbackFn)
map(callbackFn, thisArg)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
callbackFn | Function | — | Function called for each element, returning a new value |
thisArg | any | undefined | Value to use as this when executing callbackFn |
The callback function receives three arguments:
| Argument | Type | Description |
|---|---|---|
element | any | Current element being processed |
index | number | Index of current element |
array | Array | Array that map was called upon |
Examples
Basic usage
const numbers = [1, 2, 3, 4, 5];
const doubled = numbers.map(n => n * 2);
console.log(doubled);
// [2, 4, 6, 8, 10]
Transforming objects
const users = [
{ name: 'Alice', age: 25 },
{ name: 'Bob', age: 30 },
{ name: 'Charlie', age: 35 }
];
const names = users.map(user => user.name);
console.log(names);
// ['Alice', 'Bob', 'Charlie']
Using index parameter
const fruits = ['apple', 'banana', 'cherry'];
const indexed = fruits.map((fruit, i) => `${i + 1}. ${fruit}`);
console.log(indexed);
// ['1. apple', '2. banana', '3. cherry']
Common Patterns
Chaining with other array methods
const numbers = [1, 2, 3, 4, 5];
const result = numbers
.filter(n => n > 2)
.map(n => n * 10);
console.log(result);
// [30, 40, 50]
Extracting specific properties
const products = [
{ id: 1, price: 100, name: 'Widget' },
{ id: 2, price: 200, name: 'Gadget' }
];
const prices = products.map(p => p.price);
console.log(prices);
// [100, 200]
Creating objects from arrays
const ids = [1, 2, 3];
const objects = ids.map(id => ({ id, status: 'active' }));
console.log(objects);
// [{ id: 1, status: 'active' }, { id: 2, status: 'active' }, { id: 3, status: 'active' }]
Key Behaviors
map()does not mutate the original arraymap()returns a new array with the same length as the originalmap()skips holes in sparse arrays but preserves themmap()always returns a new array—even if all elements remain unchanged
See Also
- array::filter — filter elements based on a condition
- array::flatMap — map then flatten in one step