# Understanding the Challenge, Iterate Through an Array with a For Loop

Understanding the Challenge, Iterate Through an Array with a For Loop
0

#1

Hi everyone. On the challenge, Iterate Through an Array with a For Loop, there is a paragraph that says, “Remember that Arrays have zero-based numbering, which means the last index of the array is length - 1. Our condition for this loop is i < arr.length, which stops when i is at length - 1.” I don’t understand it. Could someone explain it to me? Thanks.

#2

What part don’t you understand?

Arrays are indexed starting at 0.
`var exampleArray = [ 'a', 'b', c'];`
In this example `'a'` is the value at index `0`. The length of this example array is `3`. The index of the last item is `2`.

`<` means “less than” so a loop with `i < arr.length` will only continue while `i` is less than the length of `arr`.

#3

@IAScoding1
Yes, tricky at first but you get used to it. Length is not zero based, but arrays are zero based.
Using the above example:
`var exampleArray = [ 'a', 'b', c'];`
exampleArray[0] is 'a’
exexampleArray[1] is 'b’
exampleArray[2] is 'c’
exampleArray[3] is ‘d’

but with the four elements:
exampleArray.length is 4

You can see if you use the length as an index there is a problem, there is no exampleArray[4], hence you will always be using .length - 1 to define the last element in the array.

Does that make sense? This is is the convention and avoids the confusion of length zero holding one element.
-WWC

#4

The part I don’t understand is that the last index of the array is length - 1. Wouldn’t the last index of the array be length 5?

#5

`.length()` gives us the number of elements in an array.

``````var arr1 = ['a', 'b', 'c']; // length is 3
var arr2 = ['lions', 'tigers', 'bears', 'OH MY!']; // length is 4
``````

The last element of any array will have an index of one less than the length.
`arr1[arr1.length -1]` is equivalent to `arr1[3 - 1]` is equivalent to `arr1[2]` which is `'c'`.

#6

Thank you. This helps.