In this lesson, we will see how loops iterate arrays, where iterate means accessing elements in the array one by one.
Iterating single-dimensional arrays
To iterate a single dimension array, we design a loop that will index each element and terminate when the end is reached. Take for example the following array:
var arr = [ 10, 25, 7, 100, 20 ];
To iterate it, we start off with the first element (index 0) and end at the last element (index 4).
Notice a couple of things:
- The indexing starts from
0. - The index in each iteration is incremented by one.
- The indexing stops at the last index, which is always one less than