+ 1
What is the difference between forEach iteration vs for loop iteration?
Web Development
2 Réponses
+ 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
+ 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