Nested Arrays and Objects

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

This Nested Arrays and Objects worksheet is part of the JavaScript Coding without HTML and CSS journey. Print it as a booklet?

Nested Arrays and Objects

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Does person.pets[0] access an element nested inside an object?
A No
B Yes
2. Can dot notation and bracket notation be chained together?
A Yes
B No
3. Does grid[0][1] access an element in a 2D array?
A No
B Yes
4. Is users.0.name valid syntax for accessing an array element?
A No
B Yes
5. Does obj.addr.city access a property nested two levels deep?
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
1obj.pets[0] AAccesses the city property nested inside addr inside obj
2obj.addr.city BWrong - a nested property needs a dot, not unquoted brackets
3Accesses an element in a 2D array using two indexes Cusers[0].name
4Accesses a property on an object stored inside an array DWrong - array indexes use brackets, not dots
5users.0.name Egrid[0][1]
6obj.addr[city] FAccesses the first element of the pets array inside obj

Definitions

obj.pets[0]

Fill in the Blanks

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

1.
console.log(person.pets_0]);
2.
console.log(obj.addr_city);
3.
console.log(users[0]_name);
4.
console.log(data.scores[_]);
5.
console.log(grid[0]_1]);

Spot the Error

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

Question 1
A console.log(objpets[0]);
B let obj = {};
C obj.pets = ["Rex", "Mia"];
Question 2
A objaddr = {city: "NYC"};
B let obj = {};
C console.log(obj.addr.city);
Question 3
A let obj = {};
B obj.pets = ("Rex", "Mia");
C console.log(obj.pets[0]);
Question 4
A let obj = {};
B obj.addr = {city: "NYC"];
C console.log(obj.addr.city);
Question 5
A let obj = {};
B obj.pets = ["Rex", "Mia"];
C console.log(obj.pets.0);

Answer Key

Nested Arrays and Objects
Section A: Quick Questions
Q1
B — Yes
Q2
A — Yes
Q3
B — Yes
Q4
A — No
Q5
A — Yes
Section B: Match the Pairs
Q1
obj.pets[0] → Accesses the first element of the pets array inside obj
Q2
obj.addr.city → Accesses the city property nested inside addr inside obj
Q3
Accesses an element in a 2D array using two indexes → grid[0][1]
Q4
Accesses a property on an object stored inside an array → users[0].name
Q5
users.0.name → Wrong - array indexes use brackets, not dots
Q6
obj.addr[city] → Wrong - a nested property needs a dot, not unquoted brackets
Section C: Definitions
Q1
obj.pets[0] — Chains dot notation and bracket notation to access a value nested inside an object.
Section D: Fill in the Blanks
Q1
console.log(person.pets[0]);
Q2
console.log(obj.addr.city);
Q3
console.log(users[0].name);
Q4
console.log(data.scores[2]);
Q5
console.log(grid[0][1]);
Section E: Spot the Error
Q1
A — Missing the dot before pets - it should be obj.pets[0].
Q2
A — Missing the dot before addr - it should be obj.addr.
Q3
B — Arrays use square brackets [], not round brackets ().
Q4
B — Object literals close with }, not ].
Q5
C — Use bracket notation obj.pets[0], not a dot, for an array index.