Objects – Creation and Access

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

Objects – Creation and Access

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Does {} create an object holding key-value pairs?
A No
B Yes
2. Does object.name access the value stored at key name?
A No
B Yes
3. Does bracket notation require quotes around the key?
A Yes
B No
4. Is () used to create an object in JavaScript?
A No
B Yes
5. Are keys and values separated by a colon in an object?
A Yes
B No

Match the Pairs

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

Left columnRight column
1{name: "Sam", age: 12} ACreates an object with two key-value pairs
2person.name BWrong - objects use curly braces, not round brackets
3Accesses a value using a key wrapped in quotes CWrong - the key in bracket notation needs quotes
4Separates a key from its value inside an object DAccesses the name value using dot notation
5let a = (name: "Sam"); E:
6console.log(person[name]); Fperson["age"]

Definitions

{}

Fill in the Blanks

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

1.
let person = _name: "Sam"};
2.
console.log(person_name);
3.
console.log(person_"name"]);
4.
let car = {color_ "red"};
5.
let dog = {name: "Rex"_;

Spot the Error

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

Question 1
A let a = (name: "Sam");
B console.log(a.name);
C let b = {age: 12};
Question 2
A let c = {name "Amy"};
B console.log(c.name);
C let d = {age: 9};
Question 3
A let e = {name: "Bo"};
B console.log(e#name);
C let f = {age: 8};
Question 4
A let g = {name: "Al"};
B console.log(g[name]);
C let h = {age: 7};
Question 5
A let i = {name: "Zo"};
B console.log(i.name);
C let j = {age: 6;

Answer Key

Objects – Creation and Access
Section A: Quick Questions
Q1
B — Yes
Q2
B — Yes
Q3
A — Yes
Q4
A — No
Q5
A — Yes
Section B: Match the Pairs
Q1
{name: "Sam", age: 12} → Creates an object with two key-value pairs
Q2
person.name → Accesses the name value using dot notation
Q3
Accesses a value using a key wrapped in quotes → person["age"]
Q4
Separates a key from its value inside an object → :
Q5
let a = (name: "Sam"); → Wrong - objects use curly braces, not round brackets
Q6
console.log(person[name]); → Wrong - the key in bracket notation needs quotes
Section C: Definitions
Q1
{} — Creates an object holding key-value pairs, which can then be accessed by key.
Section D: Fill in the Blanks
Q1
let person = {name: "Sam"};
Q2
console.log(person.name);
Q3
console.log(person["name"]);
Q4
let car = {color: "red"};
Q5
let dog = {name: "Rex"};
Section E: Spot the Error
Q1
A — Objects use curly braces {}, not round brackets ().
Q2
A — Missing the colon between the key and its value.
Q3
B — Dot notation uses a period, not a #, before the key.
Q4
B — A key used in bracket notation needs quotes around it.
Q5
C — The object is missing its closing curly brace.