When I loop an array or object I frequently use this way:
var arrCars = ["opel" , "audi" , "volvo" , "volkswagen" ,
"renault" , "porsche" , "infinity" , "lexus"];
for (var i = 0, a; a = arrCars[i]; i++) {
alert(a);
}
It gives clear short code in my opinion instead of e.g. this:
for (var i = 0; i < arrCars.length; i++) {
alert(arrCars[i]);
}
In this example it isn't a big issue but when there are some nested objects etc. it gives a lot code.
There are many syntax possibilities like you can see under this question here. But I still can't find out how I can loop trough a part of an array. For example, how can I loop only the first 3 values with my preferred syntax. Ofcourse i can use an extra condition statement, but I'm wondering how I can include this condition in the for syntax itself like: "for (var i = 0; i < 4; i++){}" if it's possible ofcourse.