What’s the difference between map() and forEach() in JS?

Arrays are super useful and widely used data structures. It also has a ton of different functions which can make our work so much easier. The functions that many people get confused about is map() and forEach() and it’s reasonable both do almost the sa…


This content originally appeared on DEV Community and was authored by Piyush Panchal

Arrays are super useful and widely used data structures. It also has a ton of different functions which can make our work so much easier. The functions that many people get confused about is map() and forEach() and it's reasonable both do almost the same thing. I personally made this mistake so many times when I was a beginner. The similarity is- that they both run a function or a piece of code for each of the element of the array. But forEach() does not return any new array. It just runs the code for the elements. map() on the other hand returns a new array with the result. Hope this helped.

Thanks for reading.


This content originally appeared on DEV Community and was authored by Piyush Panchal


Print Share Comment Cite Upload Translate Updates
APA

Piyush Panchal | Sciencx (2021-02-13T16:00:03+00:00) What’s the difference between map() and forEach() in JS?. Retrieved from https://www.scien.cx/2021/02/13/whats-the-difference-between-map-and-foreach-in-js/

MLA
" » What’s the difference between map() and forEach() in JS?." Piyush Panchal | Sciencx - Saturday February 13, 2021, https://www.scien.cx/2021/02/13/whats-the-difference-between-map-and-foreach-in-js/
HARVARD
Piyush Panchal | Sciencx Saturday February 13, 2021 » What’s the difference between map() and forEach() in JS?., viewed ,<https://www.scien.cx/2021/02/13/whats-the-difference-between-map-and-foreach-in-js/>
VANCOUVER
Piyush Panchal | Sciencx - » What’s the difference between map() and forEach() in JS?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/02/13/whats-the-difference-between-map-and-foreach-in-js/
CHICAGO
" » What’s the difference between map() and forEach() in JS?." Piyush Panchal | Sciencx - Accessed . https://www.scien.cx/2021/02/13/whats-the-difference-between-map-and-foreach-in-js/
IEEE
" » What’s the difference between map() and forEach() in JS?." Piyush Panchal | Sciencx [Online]. Available: https://www.scien.cx/2021/02/13/whats-the-difference-between-map-and-foreach-in-js/. [Accessed: ]
rf:citation
» What’s the difference between map() and forEach() in JS? | Piyush Panchal | Sciencx | https://www.scien.cx/2021/02/13/whats-the-difference-between-map-and-foreach-in-js/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.