When to use map() vs. forEach()

April 24, 2025

A laptop with a blank screen sits on an outdoor desk at sunset, flanked by a phone and a pen holder with pens and pencils. A railing and blurred landscape are visible in the background.

I saw this post on Reddit about a JavaScript coding assessment and it got me thinking. A common task developers perform is iterating over arrays. And two of the most frequently used methods for this are map() and forEach(). Both seem similar, but the differences can significantly affect how your code behaves. Let’s take a closer look at both and why I think map() wins out as the better choice when transforming data.

Source: When to use map() vs. forEach() – Matt Smith

To a man with a hammer, everything is a nail–and for developers (I include myself in this) once we learn an approach to solving a problem, such as iterating over an array, we tend to keep using that approach even when better alternatives emerge.

Here Matt Smith looks at forEach and map for iterating over arrays. When to use each approach and why.