Looping through an array by index
A loop that counts i from 0 up to the last index, so scores[i] visits every item in the array. Use < not <=, or i goes one step past the end and reads undefined.
Understanding Looping through an array by index in ACARA Digital Technologies Years 9–10
What it does: scores[i] uses the counter i as the index, so each pass of the loop reads the next item. The last valid index is length - 1, so the condition must use < and not <=. Array indexes start at 0, so scores[0] is the first item. Starting at 1 would skip it. Indexes run 0, 1, 2, 3. scores[4] is past the end and gives undefined. With <=, i reaches 3. scores[3] does not exist, so it prints undefined as an extra, unwanted step. Using length makes the loop adjust to the array size. Fixed numbers are allowed but break easily when the array changes.
Common mistakes: This adds the counter i instead of the item at that index, so the total is 0 + 1 + 2 = 3. It should be sum += pts[i]; to add each score. The counter starts at 1, so index 0 is skipped and the first score is never changed. A loop over every item must start at i=0.
Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.
This module's key terms are also in a printable crossword (Modules 681–707) and word search (Modules 629–707).
Take this worksheet further — free with Cloodit
Each concept is reinforced from every angle — learned, then applied, then tested.
Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.
Over 100 of them, across seven natural biomes.
Turns their work into revision material ready for Flash Cards and Exam study.
Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.
Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.
Quick Questions
Match the Pairs
Key Term
for (let i = 0; i < scores.length; i++)Fill in the Blanks
Spot the Error
Student with a class code? Join your class →