ForEach vs Map function

In simple words,

ForEach allows you to iterate over the list of items.

Map function allows you to perform Iteration AND transform the items, you can choose whether you want to transform or do you just want to iterate.

Example —

ForEach in the below example is iterating over the list, If we would want to transform each item like ‘Hi Dog’, ‘Hi Cats!’, we would have to iterate like below, create another variable and assign the transformed value to it.

forEach example

If we choose to work with Map instead, it not only allows us to iterate but also to transform if we want.

Map transformation example

If we want to do only iteration, we can do

Map function iteration example

This is my Day 5/100 of learning and getting better with Javascript and React.

See you on Day 6!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store