JavaScript forEach vs map, key differences?
forEach vs map, key differences
December 1, 2024As a programmer, we deal with different sorts of data structures, and one of the most common data structures is an array, or in other programming languages, a list.
If you've been programming in JS for a while, you've probably come across different methods to loop through an array.
In this article, I'd like to review these two methods, forEach and map, and see how they are different from each other.
Key differences
Purpose
- forEach() is used to execute side effects for each element in an array, meaning it's used for actions like logging, updating DOM, storing values, etc.
- map() is used to create a new array based on the original array, but with each element transformed, e.g. multiplying each element by 2.
Return value
- forEach() returns undefined.
- map() returns a new array.
Chainability
forEach() is not chainable, while map() is chainable, for example:
React use cases, why map() is preferred?
We often use map() in React to transform an array of data into a list of JSX elements, the main reason is that map() returns a new array, each element can be JSX elements, this fits the concept of componentization and declarative rendering.
- The return value can be using rendering directly.
- Cleaner and more declarative, because when we use map(), we know that we want to convert each value from an array into a JSX element, and we don't need to manage the array or index manually.
-
React is single data source, it means that the data is stored in the state, and the state is the single source of truth, using map() can maintain the data consistency because it returns a new array, and it's easier to track and update.
-
Using map() is more flexible, because it can be used with other array methods, e.g. filter(), reduce(), etc.