I’ve been writing a lot of JS lately, and I wanted to take this opportunity to drop some knowledge right here.
Although it may be tempting to use this as a way to iterate over an Array, this is a bad idea. The
for...instatement iterates over user-defined properties in addition to the array elements, so if you modify the array’s non-integer or non-positive properties (e.g. by adding a
"foo"property to it or even by adding a method or property to
for...instatement will return the name of your user-defined properties in addition to the numeric indexes. Also, because order of iteration is arbitrary, iterating over an array may not visit elements in numeric order. Thus it is better to use a traditional for loop with a numeric index when iterating over arrays.