Changing and adding object properties

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Changing and adding object properties

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. const student = { name: 'Ava', age: 15 }; student.age = 16; What is student.age now?
A 16
B 15
2. Which line changes the age property of student to 16?
A student.age = 16;
B student.age == 16;
3. const student = { name: 'Liam' }; student.house = 'Blue'; What happens?
A A new house property is added to student
B An error is thrown because house did not exist
4. const pet = { name: 'Rex' }; console.log(pet.age); What is printed?
A An error message
B undefined
5. True or false: you can change a property of an object that was declared with const.
A False
B True

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1student.age = 16; Aconsole.log(student.email); // undefined
2student.house = "Red"; Bstudent.points = 50;
3Reading a property that was never added gives undefined CChanges the existing age property of student to 16
4Use dot notation and = to give an object a new property DAdds a new house property to student if it was not there before
5student.age == 16; E== compares values instead of assigning. It does not change age. Use a single = to assign.
6student = 16; FThis assigns to the whole variable, not the age property. It should be student.age = 16;

Definitions

student.age = 16

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
let student = { name: "Ava", age: 15 }; student.age _ 16; console.log(student.age);
2.
let student = { name: "Ava", age: 15 }; _______.age = 16; console.log(student.age);
3.
let student = { name: "Ava" }; student.age = 16; console.log(student.___);
4.
let student = { name: "Ava" }; console.log(student.age); // prints _________
5.
let student = { name: "Ava", age: 15 }; student.age __ 1; // age is now 16

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A const student = { age = 15 };
B student.age = 16;
C console.log(student.age);
Question 2
A const pet = { name: 'Rex' };
B pet.age = 3;
C pet.age = 4;
D console.log(pet.Age);
Question 3
A const car = { speed: 0 };
B car.speed = 50;
C car.colour == 'red';
D console.log(car.colour);
Question 4
A const dog = { name, 'Max' };
B dog.age = 2;
C console.log(dog.age);
Question 5
A const book = { pages: 100 };
B book.pages = 120;
C book.title: 'Maps';
D console.log(book.title);

Answer Key

Changing and adding object properties
Section A: Quick Questions
Q1
A — 16
Q2
A — student.age = 16;
Q3
A — A new house property is added to student
Q4
B — undefined
Q5
B — True
Section B: Match the Pairs
Q1
student.age = 16; → Changes the existing age property of student to 16
Q2
student.house = "Red"; → Adds a new house property to student if it was not there before
Q3
Reading a property that was never added gives undefined → console.log(student.email); // undefined
Q4
Use dot notation and = to give an object a new property → student.points = 50;
Q5
student.age == 16; → == compares values instead of assigning. It does not change age. Use a single = to assign.
Q6
student = 16; → This assigns to the whole variable, not the age property. It should be student.age = 16;
Section C: Definitions
Q1
student.age = 16 — Assigning to object.property with = changes an existing property or adds a new one if it is not there yet. Reading a property that does not exist gives undefined.
Section D: Fill in the Blanks
Q1
let student = { name: "Ava", age: 15 }; student.age = 16; console.log(student.age);
Q2
let student = { name: "Ava", age: 15 }; student.age = 16; console.log(student.age);
Q3
let student = { name: "Ava" }; student.age = 16; console.log(student.age);
Q4
let student = { name: "Ava" }; console.log(student.age); // prints undefined
Q5
let student = { name: "Ava", age: 15 }; student.age += 1; // age is now 16
Section E: Spot the Error
Q1
A — Inside an object literal, each property is written as name: value with a colon. It should be { age: 15 }. The = sign is only used when assigning with dot notation after the object exists.
Q2
D — Property names are case-sensitive. The property was added as age, so pet.Age does not exist and gives undefined. It should be pet.age.
Q3
C — A double == compares values; it does not add or change a property. Use a single = to assign: car.colour = 'red'; adds the new colour property.
Q4
A — A property name and its value are separated by a colon, not a comma. It should be { name: 'Max' }.
Q5
C — A colon is only used inside the curly braces of an object literal. To add a property afterwards, use assignment with =: book.title = 'Maps';