← ACARA Digital Technologies Years 9–10

Reading beyond the end of an array

Reading an index past the last item (or a negative index) does not crash. It gives undefined. The last valid index is length - 1, so scores[10] in a 10-item array is undefined.

Understanding Reading beyond the end of an array in ACARA Digital Technologies Years 9–10

What it does: With 10 items the indexes run 0 to 9, so length - 1 reaches the last item safely. length is one more than the last index, so this is an off-by-one mistake that returns undefined. The array has 10 items, so the valid indexes are 0 to 9. Index 10 does not exist, so JavaScript gives undefined. Counting from 1 instead of 0 is a classic off-by-one mistake that reads past the end. The <= condition runs one time too many, causing an off-by-one error that prints undefined at the end.

Common mistakes: The array now has only 3 items, so the last valid index is 2. scores[3] is past the end and gives undefined instead of 8. The array should have 4 items: [6, 9, 7, 8]. i <= n makes the loop run once too often. When i is 3, m[3] is undefined and sum becomes NaN. It should be i < n.

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
let scores = [5, 8, 9, 7, 6, 10, 4, 3, 8, 9]; What does console.log(scores[10]) show?
undefined9
Matches
let colours = ['red', 'blue']; console.log(colours[2]);
↓
The array has 2 items, so the indexes are 0 and 1. colours[2] is undefined. Use colours[1] for the last item.
Applying
Meanings
Term
arr[arr.length]
Meaning
Reading an index past the last item (or a negative index) does not crash. It gives undefined. The last valid index is length - 1, so scores[10] in a 10-item array is undefined.
Practice
const scores = [80, 65, 92];
Testing
Gaps
console.log(scores[]);
Check Answer
Spots
1const scores = [6, 9, 7];
2const last = scores[3];

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

The 'Penguin' badge — List Waddler, 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. let scores = [5, 8, 9, 7, 6, 10, 4, 3, 8, 9]; What does console.log(scores[10]) show?
A. 9
B. undefined ✓
The array has 10 items, so the valid indexes are 0 to 9. Index 10 does not exist, so JavaScript gives undefined.
2. What happens when you read an index that does not exist, like colours[5] in a 3-item array?
A. It gives undefined and the program keeps running ✓
B. It throws an error and the program stops
Reading a missing index does not crash JavaScript. It just gives undefined.
3. Which expression gets the LAST item of an array called names?
A. names[names.length - 1] ✓
B. names[names.length]
Indexes start at 0, so the last valid index is length - 1. names[names.length] is one past the end and gives undefined.
4. let pets = ['cat', 'dog', 'fish']; What does console.log(pets[3]) show?
A. undefined ✓
B. fish
The items are at indexes 0, 1 and 2. Index 3 is past the end, so the result is undefined.
5. let pets = ['cat', 'dog', 'fish']; What does console.log(pets[-1]) show?
A. fish
B. undefined ✓
JavaScript square brackets do not count backwards from the end. A negative index like -1 does not exist, so it gives undefined.

Match the Pairs

let scores = [5, 8, 9]; console.log(scores[3]); → Prints undefined, because a 3-item array only has indexes 0, 1 and 2
console.log(scores[-1]); → Prints undefined, because a negative index is not a valid position
The last valid index of an array is its length minus 1 → scores[scores.length - 1]
Using length as the index reads past the end and gives undefined → scores[scores.length]
let colours = ['red', 'blue']; console.log(colours[2]); → The array has 2 items, so the indexes are 0 and 1. colours[2] is undefined. Use colours[1] for the last item.
for (let i = 0; i <= scores.length; i++) { console.log(scores[i]); } → The loop uses <= so the last pass reads scores[scores.length], which is undefined. It should use i < scores.length.

Key Term

arr[arr.length]
Reading an index past the last item (or a negative index) does not crash. It gives undefined. The last valid index is length - 1, so scores[10] in a 10-item array is undefined.

Fill in the Blanks

1. let scores = [5, 8, 6]; console.log(scores[3]);
Hint: Type the first index that is past the last item of this 3-item array.
2. let scores = [5, 8, 6]; console.log(scores[scores.length]);
Hint: Type the array property that gives the number of items. Using it as an index reads past the end.
3. let scores = [5, 8, 6]; console.log(scores[-1]);
Hint: Type a negative index. In a plain array it does not count from the end, so it gives undefined.
4. let scores = [5, 8, 6]; console.log(scores[scores.length - 1]);
Hint: Type the operator that turns the length into the last valid index.
5. let scores = [5, 8, 6]; let x = scores[5]; console.log(x === undefined);
Hint: Type the value you get back when you read an index that does not exist.

Spot the Error

Question 1
A const scores = [6, 9, 7];
B const last = scores[3];
C console.log(last); // 8
Line A — The array now has only 3 items, so the last valid index is 2. scores[3] is past the end and gives undefined instead of 8. The array should have 4 items: [6, 9, 7, 8].
Question 2
A const m = [70, 85, 90];
B const n = m.length;
C let sum = 0;
D for (let i = 0; i <= n; i++) {
E sum += m[i];
F }
Line D — i <= n makes the loop run once too often. When i is 3, m[3] is undefined and sum becomes NaN. It should be i < n.
Question 3
A const s = [4, 6, 9];
B const last = s[-1];
C console.log(last);
Line B — JavaScript arrays do not support negative indexes like s[-1]. It gives undefined. Use s[s.length - 1] to get the last item.
Question 4
A const pets = ["cat", "dog"];
B console.log(pets[2]); // dog
C console.log(pets.length); // 2
Line B — Indexes start at 0, so the second item is pets[1]. pets[2] is past the end of a 2-item array and gives undefined. It should be pets[1].
Question 5
A let i = 3;
B const items = ['a', 'b', 'c'];
C console.log(items[i]); // c
Line A — The array has 3 items, so its last index is 2. items[3] is past the end and gives undefined, not 'c'. It should be let i = 2;

Student with a class code? Join your class →