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.
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
arr[arr.length]Fill in the Blanks
Spot the Error
Student with a class code? Join your class →