They are best for iterating over Javascript or JSON objects, like {"foo":bar,"baz":boo}
, but not for arrays, see more here for more info about that.
They are best done like this:
var obj={"foo":bar,"baz":boo}
for(var key in obj){
//hasOwnProperty ensures that it exists and bad stuff doesn't happen with deleted properties
if(obj.hasOwnProperty(key)){
obj[key].doSomething();
}
}
Douglas Crockford has a very good page on this here. For arrays, they should never be used, but they are very useful for objects, especially if you don't know what order the keys will be in, or what keys will be there. For arrays, just do this
for(var i=0;i<array.length;i++){
//do some stuff
}