Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
March 29, 2022 05:22 pm GMT

.forEach vs .map

Working with JavaScript, especially starting out, has been quite the challenge. An entertaining one at that, but definitely quite an endeavor. To start off my beautiful blogging journey, what better a way to start of then explaining two array-manipulation techniques?

Let's start of with .forEach, a function that allows the user to transverse through an array and issue a callback function once for each element in an array. It does this in the order of the array, executing each element one at a time.

And for the second we have .map, which creates a new version of an array for you to execute a function on. Similar to .forEach, it will call a function on each element in the array.

In short, one large difference that took me one whole hour to internally process, is that .forEach will affect the original array and not return anything, whereas .map will create a new array that is a copy of the original to execute the function on and return the new array.


Original Link: https://dev.to/benjaminkane03/foreach-vs-map-8ci

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To