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.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I would like to clarify that, regarding point 1,
map()
andforEach()
do not expect the same type of functions.forEach()
expects a function that takes in one argument and returns nothing. Passing in the functionx => 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
andforEach
to just not confuse folks. But, if you read my final thought, you'll see thatmap
is all about transforming data, and this example should be done withmap
.