Learn JavaScript
❯ arrays.map

map: Transforming Arrays in JavaScript

$concept

map builds a new array by applying a function to every element: [1,2,3].map(x => x * 2) is [2,4,6]. The original never changes (map is pure when its callback is), the result keeps order and length, and the callback receives (value, index, array) for context-aware transforms.

map is for one-to-one transformation — same length in and out. Filtering belongs to filter, side effects to forEach, accumulation to reduce; reaching for map for side effects (ignoring its return) is the classic misuse linters flag.

$how it works
1

One in, one out

Result length always equals input length — each element maps to exactly one output.

2

Pure and non-mutating

The input array is untouched; a fresh array returns — chainable, testable, safe.

3

Callback gets context

(value, index, array) parameters enable position-aware transforms without external counters.

4

Not for side effects

Ignoring map's return to run effects misuses it — forEach (or for...of) expresses iteration-with-effects honestly.

$example
let prices = [10, 20, 30];
let withTax = prices.map(p => p * 1.1);
console.log(withTax);
console.log(prices.length);
Output
[ 11, 22, 33 ]
3
1The arrow multiplies each price by 1.1 into a new array.
2withTax prints transformed; prices still holds the original three — untouched.
3Same length, new values, pure transform — map's contract fulfilled.
$use cases
Rendering lists (users.map(u => <Row/>) in React)
Converting units, currencies, and formats element-wise
Extracting fields (rows.map(r => r.name)) from object arrays
Chaining into filter/reduce pipelines

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in JavaScript yourself — building beats watching.