← ACARA Digital Technologies Years 9–10

The array length property

A property (no brackets) giving how many items are in an array. The last index is length - 1, an empty array has length 0, and length grows as items are added.

Understanding The array length property in ACARA Digital Technologies Years 9–10

What it does: Using names.length with no brackets gives the number of items stored in the names array. Indexes start at 0, so the last index is length - 1. scores[scores.length] is index 4, which is undefined. push adds an item, so length grows by 1. It went from 2 to 3. pop removes the last item, so length shrinks by 1. The length is now 2. JavaScript keeps length up to date for you whenever the array changes. Adding () tries to call length as a function, which causes a TypeError because length is a number.

Common mistakes: {} makes an empty object, not an array, so push() fails and items.length is undefined. An empty array is written [] and starts with length 0. The parameter is named list, but the body uses arr, so arr is not defined. The parameter must be arr to match arr.length.

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 pets = ['cat', 'dog', 'fish']; Which gives the number of items in pets?
pets.length()pets.length
Matches
let total = fruits.length();
↓
length is a property, not a method, so it must not have brackets. Write fruits.length.
Applying
Meanings
Term
array.length
Meaning
A property (no brackets) giving how many items are in an array. The last index is length - 1, an empty array has length 0, and length grows as items are added.
Practice
let scores = [80, 92, 75];
Testing
Gaps
console.log(scores.);
Check Answer
Spots
1let items = {};
2items.push('pen');

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

The 'Moon Jellyfish' badge — Orbital Streak, 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 pets = ['cat', 'dog', 'fish']; Which gives the number of items in pets?
A. pets.length ✓
B. pets.length()
length is a property, not a method, so it is written without brackets. pets.length gives 3.
2. let scores = [10, 20, 30, 40]; Which expression gets the LAST item?
A. scores[scores.length - 1] ✓
B. scores[scores.length]
Indexes start at 0, so the last index is length - 1. scores[scores.length] is index 4, which is undefined.
3. What does this print? let list = []; console.log(list.length);
A. 0 ✓
B. undefined
An empty array still has a length property. Its value is 0 because there are no items.
4. let games = ['chess', 'tag']; games.push('soccer'); What is games.length now?
A. 2
B. 3 ✓
push adds an item, so length grows by 1. It went from 2 to 3.
5. An array has 5 items. What is the index of its last item?
A. 5
B. 4 ✓
Indexes start at 0, so five items use indexes 0 to 4. The last index is length - 1.

Match the Pairs

scores[scores.length - 1] → Gets the last item in the scores array, because the last index is always length - 1
let items = []; console.log(items.length); → Prints 0, because an empty array has a length of 0
length is a property, so it is written without brackets to give the number of items → let count = names.length;
The length grows when an item is added to the array → let pets = ['cat']; pets.push('dog'); console.log(pets.length);
let total = fruits.length(); → length is a property, not a method, so it must not have brackets. Write fruits.length.
let last = colours[colours.length]; → The last index is length - 1, so this reads past the end and gives undefined. Write colours[colours.length - 1].

Key Term

array.length
A property (no brackets) giving how many items are in an array. The last index is length - 1, an empty array has length 0, and length grows as items are added.

Fill in the Blanks

1. let scores = [5, 8, 9]; console.log(scores.length);
Hint: Type the property name (no brackets) that gives how many items the array holds.
2. let scores = [5, 8, 9]; console.log(scores[scores.length - 1]);
Hint: Type the number you subtract from the length to get the last index.
3. let list = []; console.log(list.length === 0);
Hint: Type the length of an array that has no items in it.
4. let pets = ["cat"]; pets.push("dog"); console.log(pets.length === 2);
Hint: Type the length after the second item has been added.
5. let nums = [4, 7, 2]; for (let i = 0; i < nums.length; i++) console.log(nums[i]);
Hint: Type the comparison operator that keeps the loop index below the length, so it stops before reading past the end.

Spot the Error

Question 1
A let items = {};
B items.push('pen');
C items.push('ruler');
D console.log(items.length);
Line A — {} makes an empty object, not an array, so push() fails and items.length is undefined. An empty array is written [] and starts with length 0.
Question 2
A function lastItem(list) {
B return arr[arr.length - 1];
C }
D console.log(lastItem([4, 8]));
Line A — The parameter is named list, but the body uses arr, so arr is not defined. The parameter must be arr to match arr.length.
Question 3
A const days = ['Mon', 'Tue'];
B days.push('Wed');
C console.log(days.length());
Line C — length is a property, not a method, so it has no brackets. It should be days.length, which gives 3.
Question 4
A const a = [5, 10, 15];
B const last = a[a.length];
C console.log(last);
Line B — The last index is length - 1 because indexes start at 0. a[a.length] reads past the end and gives undefined. It should be a[a.length - 1].
Question 5
A const queue = [];
B queue.push('Sam');
C console.log(queue.lenght);
Line C — length is misspelled as lenght, so it gives undefined. It should be queue.length, which prints 1 after one push.

Student with a class code? Join your class →