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

5 Responses to “JavaScript Array For Loop Example”

  1. my self Says:

    wow

  2. Pietje Puk Says:

    You Rule!!

  3. google pagerank Says:

    Hi,

    Nice post..informative..thanks

  4. Is Says:

    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.

  5. ReTox Says:

    Absolutely fantastic!

Leave a Reply