Why is using "for...in" for array iteration a bad idea? Why is using "for...in" for array iteration a bad idea? arrays arrays

Why is using "for...in" for array iteration a bad idea?


The reason is that one construct:

var a = []; // Create a new empty array.a[5] = 5;   // Perfectly legal JavaScript that resizes the array.for (var i = 0; i < a.length; i++) {    // Iterate over numeric indexes from 0 to 5, as everyone expects.    console.log(a[i]);}/* Will display:   undefined   undefined   undefined   undefined   undefined   5*/