Originally posted on my blog
JavaScript has some handy methods which help us iterate our arrays. The two most used for iteration is Array.prototyp...
For further actions, you may consider blocking this person and/or reporting abuse
I love this kind of small informative articles.
Glad you like it.
Liked the post. I think the easiest point for me to remember the difference is how I would use the two function names in a sentence. For a .map() example, "I'm going to this new land area out." This sentence implies the production of some item, a new map of a new land area. Just like the function .map(), we will have a new item mapped out after the action is done. For forEach(), "For each lemon I have, I'm going to cut it in half." Here for each implies that I'm going to perform an action on each individual lemon. Just like the function call itself.
For most function names I try to do some kind of relation to their context in a sentence.
After all, I bet there was probably a bit of though that was put into the naming of these functions by the JS architect who designed them.
ππ
ππThe way of differentiating thing is just funny. I should give it a try.
Did not read the article, but the title reminded me of code I found were people used map instead of foreach.
Which whenever it happens makes me want to headbut my screen in excited anger.
Iβll read the article now, but my bruised forehead wants to thank you in advance.
Thank you so much. Hopefully, you'll find value on it
Great article I have found mdn is great but sometimes you get those really vague explanations of things. MDN is also not the end all be all it's kind of like the wikipedia of Js css etc. I say that because it's editable by you and my right now. Sometimes there is validation of what you say is true when you are putting it in but not when I updated some css docs.
I totally agree. Sometimes, things become clear once you test it by yourself. But overall, MDN is great.
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
.Thank you for the very handy article and the great explanation. I am just learning about both these methods for the first time, and while I assumed there was at least one difference (why else have two methods), the tutorial I am following and MDN unfortunately were both rather vague on the matter. So this was a great explanation that came at exactly the right time. Thanks for writing it.
Your comment means a lot to me. I'm glad you find value on it. And thanks again for reading it.
really nice post :) thank you for sharing
I'm glad you like it. Thanks again for reading it
Very nice post
Thanks, I'm glad you like it.
Knowing the difference between the two in terms of mutability and performance will certainly help. Thank you for writing this informative article.
You're welcome. Thanks for reading it too
Very handy information. Thank you for the article π
You're welcome
Greatβ€