DEV Community

Ahmadou Waly NDIAYE
Ahmadou Waly NDIAYE

Posted on

Why so much ".forEach() VS .map()" posts in javascript?

I saw many posts comparing .forEach() and .map(), but I have never seen a situation where we are faced with a problem of choice between these 2 APIs.

Even if they both iterate through elements in an array, they stays totally different.

  • The map() method creates a new array.
  • The forEach() method executes a provided function once for each array element.

Top comments (1)

Collapse
 
gmartigny profile image
Guillaume Martigny

People are not comfortable with functional programming I think. Both functions browse the array so new devs use the two indistinctively. You appear to grasp the difference between the two so good for you. 😀