JavaScript for loop Index becomes arrayIndex

I have the following JavaScript code fragment

var myArray = new Array();
function calculateTime(ip) {
  for (i in ip) {
    window.alert(i);
    if (!i in myArray) {
      myArray[i] = 0;
    } else {
      myArray[i] += 1;
    }
  }
}

I expect i to be an index but sometimes window.alert prints "arrayIndex" and because of that my code doesn’t work correctly. Can someone explain me the reason?


Source: javascript

Leave a Reply