How do i know whats the length or my array?


I am abit confused on how the programme calculates the length of an array.
Pls help.

for example:

var cities = ["mel", "aus" , "bang" , "Hi"]
for( i = 0; i < cities.length; i++) {
console.log(cities[i]) }

So is the length actually 3 or 4? as i read that it starts from 0 because the zero based indexing.

Pls assist. Thank you.


well, one simply way to find out:

var cities = ["mel", "aus" , "bang" , "Hi"]

log the length to the console, it should show 4. the length is 4, the items are at index 0,1,2 and 3, which is why you let the loop run for values lesser then (<) the length (4)


The length is 4, but there is a difference between 'length' and 'index'. Length is from the beginning to the end, so if you were to print, console.log(cities.length);, you would receive 4 as your answer. On the other hand, the index is how the program stores things in your array, list, etc. Much like an index in a back of a book. :slight_smile:


amazing, thank you so much