loading...

re: 4 main differences between forEach() and map() you should know VIEW POST

FULL DISCUSSION
 

I would like to clarify that, regarding point 1, map() and forEach() do not expect the same type of functions. forEach() expects a function that takes in one argument and returns nothing. Passing in the function x => x * x wouldn't even be valid in many other languages with a similar function. map() instead expects a function that takes in one argument and returns a new value of any type.

Since forEach() is a void function (undefined in JS) it should preferably only be used for IO operations where no return value is expected (eg. changing the DOM or printing to console).

 

Thanks, for your comment I intentionally use the same example for both map and forEach to just not confuse folks. But, if you read my final thought, you'll see that map is all about transforming data, and this example should be done with map.

code of conduct - report abuse