Code Literacy

Unit 5 · Data and algorithms

Why arrays start at 0

An index counts steps from the start of the list. The first item is zero steps away, so it is list[0]. In a list of three, the indexes are 0, 1 and 2; list[3] is past the end and gives undefined.

The mix-up

What people often think

“The first item in a list is item number 1.”

What the tutor says

Arrays count from 0. The index means "how many steps from the start", and the first item is zero steps away.

If eggs is groceries[0], what is the index of the last item in a list of three?

How it works

The second item is list[1] and the third is list[2]. The last item is always at list.length - 1.

If code reads the wrong person from a queue, change the index you read, not the order of the data. The list was usually right all along.

An example

What worksJavaScript
const groceries = ["eggs", "milk", "bread"];
groceries[0];                     // "eggs"
groceries[1];                     // "milk"
groceries[groceries.length - 1];  // "bread"
groceries[3];                     // undefined
Three items, indexes 0 to 2.

Close cousins of this mix-up

The tutor watches for these in the same lesson.

  • “The last item in a list of three is at index 3.”

    With three items the indexes are 0, 1 and 2. groceries[3] is past the end, so JavaScript gives back undefined.

  • “The second item is groceries[2].”

    Counting starts at 0, so the second item is groceries[1]. groceries[2] is the third.

Practise it with a tutor watching

Arrays and objects

Store a list in an array, a record in an object, and read any item back out.

20 minutes See it Build it Break it Say it
Start the lesson

Free with an account. The first two units need no account.

© 2026 webfaCeMEdia. Code Literacy™ is a trademark of webfaCeMEdia.