Array Itteration and console.log

Array Itteration and console.log
0

#1

Hi Everyone. just doing some practice and i am running into some issues with console.log

var numbers = []
for(var i =0; i<=9; i++){
numbers.push(i);
}
console.log(numbers[10])

My array prints out 0-9 in this loop, but when I try to console.log the last item in this array, item 10, it says undefined. Not to sure where the problem is.

Thoughts?


#2

Arrays are zero-indexed, which means the index starts at 0. The last index in an array will be one less than the number of elements in the array. Your numbers array has 10 elements, so the last index is 9. When you try to reference an index of 10, it does not exist, so you get undefined.


#3

Thanks for the help randell! much appreciated