← ACARA Digital Technologies Years 9–10

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.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

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.

Learning
Duo
Which loop header visits every item in the array scores?
for (let i = 0; i < scores.length; i++)for (let i = 0; i <= scores.length; i++)
Matches
for (let i = 0; i <= scores.length; i++)
↓
Using <= lets i reach scores.length, which is one step past the end, so scores[i] is undefined on the last pass. Use < instead.
Applying
Meanings
Term
for (let i = 0; i < scores.length; i++)
Meaning
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.
Practice
const scores = [72, 85, 90, 64];
Testing
Gaps
for (let i = 0; i < scores.; i++) {
Check Answer
Spots
3for (let i=0;i<pts.length;i++)
4 sum += i;

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Barreleye Fish' badge — Clear Vision, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which loop header visits every item in the array scores?
A. for (let i = 0; i < scores.length; i++) ✓
B. for (let i = 0; i <= scores.length; i++)
Use < with length. The last index is length - 1, so <= goes one step too far and scores[i] becomes undefined.
2. What should the counter i start at to begin with the first item of an array?
A. 1
B. 0 ✓
Array indexes start at 0, so scores[0] is the first item. Starting at 1 would skip it.
3. scores has 4 items. Which is the index of the last item?
A. 3 ✓
B. 4
Indexes run 0, 1, 2, 3. scores[4] is past the end and gives undefined.
4. Which line inside the loop reads the current item?
A. console.log(scores.i);
B. console.log(scores[i]); ✓
Use square brackets with the counter, scores[i], so the index changes each time. scores.i looks for a property named i.
5. Which line adds 5 bonus points to every score, changing the items in place?
A. scores[i] = scores[i] + 5; ✓
B. scores = scores[i] + 5;
Assigning to scores[i] changes the item at that index. The wrong line overwrites the whole array with one number.

Match the Pairs

for (let i = 0; i < scores.length; i++) → Counts i from 0 up to the last index so every item in the scores array can be visited
scores[i] = scores[i] + 5; → Changes the item at index i in place by adding 5 to it
Use the loop counter as the index to read each item and print it → for (let i = 0; i < scores.length; i++) { console.log(scores[i]); }
Stop the loop before i reaches the array length, so no index goes past the end → i < scores.length
for (let i = 0; i <= scores.length; i++) → Using <= lets i reach scores.length, which is one step past the end, so scores[i] is undefined on the last pass. Use < instead.
for (let i = 1; i < scores.length; i++) → Starting i at 1 skips the first item at index 0. The counter should start at 0.

Key Term

for (let i = 0; i < scores.length; i++)
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.

Fill in the Blanks

1. for (let i = 0; i < scores.length; i++) { console.log(scores[i]); }
Hint: Type the array property that tells you how many items it holds.
2. for (let i = 0; i < scores.length; i++) { console.log(scores[i]); }
Hint: Type the comparison operator that stops the loop before i goes past the last index.
3. for (let i = 0; i < scores.length; i++) { console.log(scores[i]); }
Hint: Type the index of the first item in an array, where the counter should start.
4. for (let i = 0; i < scores.length; i++) { console.log(scores[i]); }
Hint: Type the counter variable that is used as the index to visit each item.
5. for (let i = 0; i < scores.length; i++) { scores[i] += 5; }
Hint: Type the operator that adds 5 to the item already stored at each index, changing it in place.

Spot the Error

Question 1
A const pts = [4, 8, 6];
B let sum = 0;
C for (let i=0;i<pts.length;i++)
D sum += i;
E console.log(sum);
Line D — 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.
Question 2
A const pts = [4, 8, 6];
B for (let i=1;i<pts.length;i++)
C pts[i] = pts[i] + 1;
D console.log(pts);
Line B — 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.
Question 3
A const a = [3, 6, 9];
B let n = 0;
C for (let i=0;i<=a.length;i++)
D n += a[i];
E console.log(n);
Line C — Using <= makes i reach 3, which is one step past the last index (2). a[3] is undefined, so n becomes NaN. It should be i<a.length.
Question 4
A const pts = [2, 4, 6];
B for (let i=0;i<pts.length;i++)
C console.log(pts(i));
Line C — Round brackets try to call pts as a function, which causes a TypeError. Items are read with square brackets: pts[i].
Question 5
A const pts = {4, 8, 6};
B for (let i=0;i<pts.length;i++)
C console.log(pts[i] * 2);
Line A — Curly braces are for objects and blocks, so this is a syntax error. An array uses square brackets: [4, 8, 6].

Student with a class code? Join your class →