Objects – for...in Loop

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

Objects – for...in Loop

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Does for...in loop over the keys of an object?
A Yes
B No
2. Is 'of' used instead of 'in' for looping over an object's keys?
A Yes
B No
3. Must the loop variable be declared with let before 'in'?
A No
B Yes
4. Does obj[key] access the value for the current key in the loop?
A Yes
B No
5. Does obj.key access the value using the loop variable's value?
A No
B Yes

Match the Pairs

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

Left columnRight column
1for (let key in person) { ... } Afor (let key in obj) { ... }
2console.log(person[key]); BWrong - use obj[key], not obj.key, to access the value
3Loops over each property name (key) of an object CAccesses the value for the current key using bracket notation
4Retrieves a value using the loop's key variable DLoops over each key in the person object
5for (let key of obj) { ... } EWrong - use 'in', not 'of', to loop over an object's keys
6console.log(obj.key); Fobj[key]

Definitions

for...in

Fill in the Blanks

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

1.
___ (let key in person) {
2.
for (let key __ person) {
3.
for (___ color in car) {
4.
___ (let prop in obj) {
5.
for (let prop __ obj) {

Spot the Error

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

Question 1
A For (let k in a) {
B let a = {x: 1};
C console.log(k);
D }
Question 2
A let b = {y: 2};
B for (let k of b) {
C console.log(k);
D }
Question 3
A let c = {z: 3};
B for (let k in c) {
C console.log(c.k);
D }
Question 4
A let d = {a: 1};
B for (let k in d) {
C console.log(k);
D ]
Question 5
A let e = {b: 2};
B for (let k in e) {
C console.log(k);
D console.log(e[k]);
E ]

Answer Key

Objects – for...in Loop
Section A: Quick Questions
Q1
A — Yes
Q2
B — No
Q3
B — Yes
Q4
A — Yes
Q5
A — No
Section B: Match the Pairs
Q1
for (let key in person) { ... } → Loops over each key in the person object
Q2
console.log(person[key]); → Accesses the value for the current key using bracket notation
Q3
Loops over each property name (key) of an object → for (let key in obj) { ... }
Q4
Retrieves a value using the loop's key variable → obj[key]
Q5
for (let key of obj) { ... } → Wrong - use 'in', not 'of', to loop over an object's keys
Q6
console.log(obj.key); → Wrong - use obj[key], not obj.key, to access the value
Section C: Definitions
Q1
for...in — Loops through each key (property name) of an object, one at a time.
Section D: Fill in the Blanks
Q1
for (let key in person) {
Q2
for (let key in person) {
Q3
for (let color in car) {
Q4
for (let prop in obj) {
Q5
for (let prop in obj) {
Section E: Spot the Error
Q1
A — 'for' must be lowercase - a capital F causes an error.
Q2
B — Use 'in', not 'of', to loop through an object's keys.
Q3
C — Use c[k], not c.k, to access the value using the loop variable.
Q4
D — The block must close with }, not ].
Q5
E — The block must close with }, not ].