map: Transforming Arrays in JavaScript
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.
One in, one out
Result length always equals input length — each element maps to exactly one output.
Pure and non-mutating
The input array is untouched; a fresh array returns — chainable, testable, safe.
Callback gets context
(value, index, array) parameters enable position-aware transforms without external counters.
Not for side effects
Ignoring map's return to run effects misuses it — forEach (or for...of) expresses iteration-with-effects honestly.
let prices = [10, 20, 30];let withTax = prices.map(p => p * 1.1);console.log(withTax);console.log(prices.length);Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in JavaScript yourself — building beats watching.