For Loop vs ForEach
Looping is the process of executing statements until that particular condition is met. Based on your problem or project looping comes in handy when you have to go through items no matter the size. When executing a for loop it usually takes three condition statements(let i = 0; i < array.length; i++) that When executing a for loop there are possibilities that you may exceed the items within your array. This is a cause of how your condition is set up, for example
. Based on your condition within the parenthesis your able to execute and alter an array of items for example.
For Each can executes the same concept as a for loop and you can be able to get the same result. The only main difference is that the callback function allows you to maintain the value. Both are syntactically written differently, rather than taking in a condition, it takes in one parameter like the for loop it is written as
. My experience using for each was mainly iterating through nested of the fetch request data.
I was able to grab elements from the array using this method.
After Es6 we did not need to access elements one by one with just only for loop. We have the option of using for each to perform the same task. It’s mainly up to the developer’s comfortability level that determines which method to use. Methods such as for each can be seen in different languages to achieve the same success.