Skip to content

map()

map() creates a new array by applying a function to every element of the original array. It does not mutate the original.

flowchart LR
A["[1, 2, 3]"]
B["map(x => x * 2)"]
C["[2, 4, 6]"]
A --> B --> C
A -.->|"Original unchanged"| A
const newArray = array.map((element, index, array) => {
return transformedElement;
});
const numbers = [1, 2, 3, 4];
const doubled = numbers.map(n => n * 2);
// [2, 4, 6, 8]
const users = [{name: 'Alice'}, {name: 'Bob'}];
const names = users.map(user => user.name);
// ['Alice', 'Bob']
  • Creates new array with transformed elements
  • Does NOT mutate original
  • Returns same length as original
  • Chainable with filter, reduce, etc.