Since an array is a collection of elements of objects, values, etc. The most common function performed on it is to iterate through each elements of the array. The most frequent way to do that is with a for loop.
Let's say you have an array with the following:
var numbers = ["one","two","three","four","five"];
The numbers array above contains five string elements, which also has a indexes associated with the it. There's also a length that is defined with each array that we will use for the for loop we are about to write. So what do the you length of the numbers array will be?
If we output the array to the browser with the statement console.log(numbers.length) do you think we will get the length to be 5 or 4?
If you guess 5 you are correct because there are five elements in the array. However, the arrays have zero based indexes therefore the index is always length -1
for(var i = 0; i < numbers.length; i++)
{
console.log(numbers[i]);
}
Let's say you have an array with the following:
var numbers = ["one","two","three","four","five"];
The numbers array above contains five string elements, which also has a indexes associated with the it. There's also a length that is defined with each array that we will use for the for loop we are about to write. So what do the you length of the numbers array will be?
If we output the array to the browser with the statement console.log(numbers.length) do you think we will get the length to be 5 or 4?
If you guess 5 you are correct because there are five elements in the array. However, the arrays have zero based indexes therefore the index is always length -1
for(var i = 0; i < numbers.length; i++)
{
console.log(numbers[i]);
}