JavaScript Array For Loop Example
If this post helps you, click an ad
The following code example demonstrates how to loop over a JavaScript array:
var aryTest = [1,2,3,4];
for (i=0;i<aryTest.length;i++)
document.write(aryTest[i]);
The length property of the JavaScript Array object is used to determine how many elements are in the array. These elements are then looped over, one-by-one, until all elements have been used, at which point the loop stops.
Related articles:
JavaScript Arrays Summary and Reference
JavaScript Array Length Property
JavaScript For Loop Example
wow
You Rule!!
Hi,
Nice post..informative..thanks
Nice blog… Here is another way I came up with:
var aryTest = new Array;
for (i=0; i<4; i++) {
aryTest[i] = 1+i;
document.write(aryTest[i]+” “);
}
//will also print 1 2 3 4.
Absolutely fantastic!