arguiot/DisplayJS

$.range() mentions for...in for arrays

Poetro opened this issue · 1 comments

The $.range() page mentions the following:

This function will be super useful when you're dealing with these kinds of for loop:

for (i in array) {
    ...
}

But the MDN explicitly notes that you should not do this:

Note: for...in should not be used to iterate over an Array where the index order is important.

Because the order of iteration is implementation-dependent, iterating over an array may not visit elements in a consistent order. Therefore it is better to use a for loop with a numeric index (or Array.prototype.forEach() or the for...of loop) when iterating over arrays where the order of access is important.
Array iteration and for...in

Yes, you are right, but you can use for...of. But in general cases, for...in will do the job, even if MDN is saying that you shouldn’t because in some cases, there is a chance to have some weird things happening if you manually set a property to an array.

I know it’s not the best practice, but it’s useful and it works with Babel 😊.