Counting in JS?

why is it when you are counting in JS for arrays do you always start with 0?

// Example
var ourArray = [1,2,3];
var ourData = ourArray[0]; // equals 1

// Setup
var myArray = [1,2,3];

// Only change code below this line.
var myData = myArray[0];

Because in JavaScript, arrays are zero-indexed. That is just the way it is.

1 Like

ok sounds great I just did not know that. good to know

Also, because arrays are zero-indexed, that means the last index in an array will be the length of the array minus 1.

For example, if I have the following array with 5 elements, the last index will be 4.

var arr = ['a', 'b', 'c', 'd', 'e'];
console.log(arr[4]); // displays the letter e in the console 
1 Like