← ACARA Digital Technologies Years 9–10

Storing objects in an array

Makes a new Student object and adds it to the end of the students array. You can then loop over the array and call each object's methods.

Understanding Storing objects in an array in ACARA Digital Technologies Years 9–10

What it does: The loop visits each object and adds its points property to the running total. Objects made from a class need the new keyword. Without new, a class constructor can't be called and JavaScript throws a TypeError. for...of loops over the array (students), not over the class name. Student is the blueprint, not the list of objects. === compares values. A single = assigns, which would overwrite every name with 'Ava' instead of testing it. First pick the object with its index, students[0], then read its property with .name. push() adds to the end of the array, and new Student(...) builds the object that gets added.

Common mistakes: Student is a class, so you must use the new keyword to make an object before pushing it. It should be list.push(new Student("Zoe"));. for...in gives the index positions ("0", "1"...), not the Student objects, so s.points is undefined and the total becomes NaN. Use for...of to visit each object.

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 743–781) and word search (Modules 743–809).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which line creates a new Student object and adds it to the end of the students array?
students.push(Student('Ava', 7));students.push(new Student('Ava', 7));
Matches
students.push(Student('Ava', 7));
↓
The new keyword is missing, so Student is called like a normal function and no object is created. It should be students.push(new Student('Ava', 7));
Applying
Meanings
Term
students.push(new Student('Ava', 7));
Meaning
Makes a new Student object and adds it to the end of the students array. You can then loop over the array and call each object's methods.
Practice
class Student {
Testing
Gaps
students.(new Student('Ava', 7));
Check Answer
Spots
2list.push(new Student("Ava"));
3list.push(Student("Zoe"));

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

The 'Galaxy Phoenix' badge — Orbit Champion, 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 line creates a new Student object and adds it to the end of the students array?
A. students.push(Student('Ava', 7));
B. students.push(new Student('Ava', 7)); ✓
Objects made from a class need the new keyword. Without new, a class constructor can't be called and JavaScript throws a TypeError.
2. After these lines run, how many objects are in the array? let students = []; students.push(new Student('Ava', 7)); students.push(new Student('Liam', 8));
A. 2 ✓
B. 0
Each push adds one object to the end of the array, so two pushes on an empty array give 2 objects.
3. Which loop visits every Student object in the students array and calls its method?
A. for (const s of students) { s.introduce(); } ✓
B. for (const s of Student) { s.introduce(); }
for...of loops over the array (students), not over the class name. Student is the blueprint, not the list of objects.
4. Inside a for...of loop with 'const s of students', how do you read one student's points?
A. s.points ✓
B. students.points
s is the single object being visited, so s.points reads that object's property. The array students has no points property.
5. You want the total of every student's points. Which is correct?
A. let total = 0; for (const s of students) { total = s.points; }
B. let total = 0; for (const s of students) { total += s.points; } ✓
+= adds each object's points to the running total. Using = just replaces the total each time, so you'd only get the last student's points.

Match the Pairs

students.push(new Student('Ava', 7)); → Creates a new Student object and adds it to the end of the students array
for (const s of students) { s.greet(); } → Visits each Student object in the array and calls its greet() method
To find the total of one property across all objects in an array, add it up inside a loop → let total = 0; for (const s of students) { total += s.points; }
To find an object by a property, loop through the array and check each object's property → for (const s of students) { if (s.name === 'Ava') { found = s; } }
students.push(Student('Ava', 7)); → The new keyword is missing, so Student is called like a normal function and no object is created. It should be students.push(new Student('Ava', 7));
for (const s of students) { total += students.points; } → It reads points from the whole array instead of the current object. It should be total += s.points;

Key Term

students.push(new Student('Ava', 7));
Makes a new Student object and adds it to the end of the students array. You can then loop over the array and call each object's methods.

Fill in the Blanks

1. students.push(new Student('Ava', 7));
Hint: Type the array method that adds an item to the end of an array.
2. for (const s of students) { s.greet(); }
Hint: Type the keyword that loops over each value in an array.
3. for (const s of students) { if (s.name === 'Ava') found = s; }
Hint: Type the operator that checks whether two values are strictly equal.
4. let total = 0; for (const s of students) total += s.points;
Hint: Type the operator that adds each object's property to the running total.
5. const students = []; students.push(new Student('Liam', 8));
Hint: Type the keyword that creates a new object from a class.

Spot the Error

Question 1
A const list = [];
B list.push(new Student("Ava"));
C list.push(Student("Zoe"));
D console.log(list.length);
Line C — Student is a class, so you must use the new keyword to make an object before pushing it. It should be list.push(new Student("Zoe"));
Question 2
A let total = 0;
B for (const s in list) {
C total += s.points;
D }
E console.log(total);
Line B — for...in gives the index positions ("0", "1"...), not the Student objects, so s.points is undefined and the total becomes NaN. Use for...of to visit each object.
Question 3
A const list = {};
B list.push(new Student("Ava"));
C list.push(new Student("Zoe"));
D console.log(list.length);
Line A — {} makes an object, which has no push method, so list.push causes a TypeError. An array of Students must be created with [] instead.
Question 4
A let found = null;
B for (const s of list) {
C if (s.name === "Zoe") {
D found == s;
E }
F }
Line D — == compares values instead of storing one. found would stay null. Use a single = to assign the matching Student: found = s;
Question 5
A const list = [];
B list.add(new Student("Ava"));
C for (const s of list) {
D s.sayHi();
E }
Line B — Arrays do not have an add method. To put a new object on the end of an array, use push: list.push(new Student("Ava"));

Student with a class code? Join your class →