Javascript why FOR IN is bad practice?

Possible duplicate:
JavaScript "For ... in" with Arrays

People always tell me that using FOR IN is bad practice, please could you explain to me why? And why is it better to use for i?

I always liked to use FOR IN, because I use PHP too, where I often use foreach, and it is very similar to FOR IN in javascript :)

+5
source share
1 answer

, . for...in - . , :

: for (string in object), object - object (, , ) string var object . Array object! , :

var array = [0,1,2];
for (var property in array)
  alert(typeof property + '\t' + property + '\n' + typeof array[property] + '\t' + array[property]);

. , []. . string, - number. var array = new Array(3). Loop , array.length == 3 .

: for...in , . for (var number = 0; number < array.length; number++) .

, JavaScript PHP (hashtables, ). var object = {string:value,anotherName:moreStuff}. for...in !

+10

All Articles