ES5 offers various loop techniques, such as for-in, for(;;), and Array.prototype.forEach(). There are however various drawbacks to each, Array.prototype.forEach() uses a callback (thus you cannot use break or return). for-in is cleaner and more readable than for(;;), but it was designed to work with objects with string keys, not arrays, and has some quirks when used with arrays.
Therefore, the new for-of syntax should be used when possible, since it avoids the pitfalls of for-in while maintaining the same simplicity and readability.
ES5 offers various loop techniques, such as
for-in
,for(;;)
, andArray.prototype.forEach()
. There are however various drawbacks to each,Array.prototype.forEach()
uses a callback (thus you cannot usebreak
orreturn
).for-in
is cleaner and more readable thanfor(;;)
, but it was designed to work with objects with string keys, not arrays, and has some quirks when used with arrays.Therefore, the new
for-of
syntax should be used when possible, since it avoids the pitfalls offor-in
while maintaining the same simplicity and readability.https://hacks.mozilla.org/2015/04/es6-in-depth-iterators-and-the-for-of-loop/