/ For Loop Issues

For loops are not always as simple as they seem.


Use for of only for Arrays, not Objects, eg:

var x = [1,2];
for (var i of x) {}; // OK
var y = {a:1};
for (var i of y) puts(i);
error: operand not an array    (at or near "a")

However, if web-portability of code is desired, using it can break older browsers.


For web-portability one might think that it is safe to use for...in instead of for of for Arrays, but there are some gotchas in some browsers:

  • Indexes are not guaranteed to be in numerical order.
  • Sometimes it will iterate over properties like length.

Thus the safest way is using for (;;).

for (;;)

Use for (;;) for Arrays if you care about portability.