+ 1

What is the difference between forEach iteration vs for loop iteration?

Web Development

24th May 2021, 3:10 PM
Edward Jackson Jr
Edward Jackson Jr - avatar
2 Answers
+ 3
forEach is mostly used with arrays, it's convenient way. You can use for loop with arrays as well, but forEach is more readable
24th May 2021, 3:13 PM
Babanyyaz
Babanyyaz - avatar
+ 1
the main difference you must be aware about array iteration is that 'forEach' iterate only non-empty slots, while 'for' iterate using a custom index value, so if you start at zero and end at length-1 iterate all slots... 'for..in' act as 'forEach' (iterate over non-empty slots) 'for..of' act as usual 'for' iteration (all slots values) what could be unobvious is concept of empty slots, wich when accessed from index return undefined as value, as do an non-empty slot assigned with undefined ;P https://code.sololearn.com/W8IlOwaRnQ62/?ref=app
24th May 2021, 7:42 PM
visph
visph - avatar