What’s The Difference Between forEach and map Methods?

Jin Vincent Necesario
3 min readSep 1, 2020

Introduction

Are you currently learning the JavaScript language and within the subject area of loops, iteration, and arrays? Plus you stumbled upon with these two methods Array.forEach() and Array.map(). Finally, confused? No worries, because in this post, we are going to answer the difference in these two methods.

Syntax of forEach & map

What is the Array.forEach?

The forEach method allows you to run a function/method for every element inside the array.

The syntax

//the syntax[].forEach(function(item, index, array){//do your stuff here…});

Quick Example

[“apple”, “mango”, “avocado”, “dragon fruit”].forEach(console.log);

Output

As you can see, we have shown the 3 arguments of the forEach method by passing the console.log method. Easy isn’t it? Will get in-depth in the later section.

What is the Array.map?

The map method returns a new set of arrays but doesn’t change the original array.

The syntax

//the syntax:[].map(function(currentValue, index,currentArray){//do your stuff here …}, thisValue)

Quick Example

[“apple”, “mango”, “avocado”, “dragon fruit”].map((currentValue) => currentValue.toUpperCase());

Output

As you can see, we have shown how the map method returns a new set of arrays in uppercase.

The difference between forEach and map

Now, that we have seen the syntax of these two array methods, we can go and answer their differences. Will do our best to explain the difference with the use of code samples. However, before going to each detail, we need some form of data.

forEach

  • Has no result value or doesn’t return anything.
  • Iterates over a list and applies some operation with side effects to each list. If you need to do something meaningful, you can do some side effects while iterating.

Output

Thoughts on the forEach method

Every time working with the forEach method. I have observed that it describes the control flow. No mystery isn’t?. Hence, we can say that it is imperative.

map

  • Returns a new list without changing anything else.
  • Has no side effects, it doesn’t change the original array-list.

Output

Thoughts on the map method

Again, going back to my observations but with the map method. I have observed that it is somewhat a flow of data. Meaning, when you have an input array then it outputs a new array through the use of this method. Hence, we can say that it is functional.

Conclusion

It is obvious that these two methods have opposing views when it comes to usage which has its own pros and cons. Therefore, we can conclude that the forEach method is using the imperative paradigm while the map method uses the functional programming paradigm.

Summary

We have seen the difference between forEach and map. We started from its syntax all the way up to the differences with code samples. I hope you have enjoyed this article, as I have enjoyed writing it. Stay tuned for more. This post was originally posted here. Many thanks, until next time, happy programming!

--

--