DEV Community

Benjamin Alan Murphy-Kane
Benjamin Alan Murphy-Kane

Posted on

.forEach vs .map

Working with JavaScript, especially starting out, has been quite the challenge. An entertaining one at that, but definitely quite an endeavor. To start off my beautiful blogging journey, what better a way to start of then explaining two array-manipulation techniques?

Let's start of with .forEach, a function that allows the user to transverse through an array and issue a callback function once for each element in an array. It does this in the order of the array, executing each element one at a time.

And for the second we have .map, which creates a new version of an array for you to execute a function on. Similar to .forEach, it will call a function on each element in the array.

In short, one large difference that took me one whole hour to internally process, is that .forEach will affect the original array and not return anything, whereas .map will create a new array that is a copy of the original to execute the function on and return the new array.

Top comments (0)