What's the difference between map and forEach?
Quick answer
map returns a new transformed array; forEach returns undefined and is used for side effects.
In detail
`map` builds a new array of the same length from each item's return value, so it's chainable and non-mutating. `forEach` just runs a callback per item, ignoring the return value, and can't be chained or broken out of. Use map to transform, forEach for side effects.
[1, 2, 3].map(n => n * 2); // [2,4,6]
[1, 2, 3].forEach(n => save(n)); // undefinedWhy interviewers ask this: Basic but reveals whether you think in transformations.
This is 1 of 118+ questions in the JavaScript Interview Kit
Get every question with detailed answers, follow-ups and real code — plus coding challenges and a last-minute revision sheet. One-time payment, instant access.
⚡ Get the JavaScript Interview Kit → ₹299