DEV Community

JCT20xx
JCT20xx

Posted on

The Use of .forEach()

So JavaScript is full of methods that make coding faster, compact, and readable. forEach() is a method that will help a lot when you have to go through lots of data that has been organized into arrays and objects and processing that data into functions. This is action is a key component for websites as the have to pull data from an API. You don't need to use .forEach() for every piece of data, like if all you need is a name then you can just drill down the array to get the name, but if you want to display a list of friends or list of groceries then you could use it. If you need to make a new list with changes to the original information then its best to use .map().

So .forEach() is the same as building a for/of loop that will iterate through an array but easier as you just need to put the array in front of the dot and you pass a function through that will apply itself to every value. You can even use this if you need to go through a nested object by drilling down and then placing the .forEach() at the end.

Top comments (0)