JavaScript Map
map() calls a function on every element of an array and returns a new array with the results. Same length, original untouched. It is the workhorse of data transformation and the middle link of the classic split, map, join pipeline.
Syntax
arr.map((x) => x * 2) Examples
| Input | Arguments | Output |
|---|---|---|
| [1, 2, 3] | x => x * 2 | [2, 4, 6] |
| ["a", "b"] | x => x.toUpperCase() | ["A", "B"] |
| ["a", "b", "c"] | (x, i) => i | [0, 1, 2] |
| ↳ The callback also receives the index as its second argument. | ||
When to use it
- Transform every item of a list (prices, labels, ids).
- Extract one field from an array of objects.
Gotchas
- map() always returns an array of the same length. Use filter() to drop elements.
- It never mutates the original array: you get a copy.
Try it live
Type your input and see Map transform it instantly.
Want to go further? Chain Map with other functions, pipe one output into the next and watch your data transform in the visual Playground.