Objects – for...in Loop, online

Six activities your students do on their own devices. See who's finished, and their scores.

Objects – for...in Loop in one minute

What students need to know

This is exactly what a for...in loop does. Bracket notation with the variable name gets each property's value.

Use 'in', not 'of', when looping over an object's properties. Just like other loops, the variable needs let (or const/var).

Mistakes students make

  • 'for' must be lowercase - a capital F causes an error.
  • Use 'in', not 'of', to loop through an object's keys.
  • Use c[k], not c.k, to access the value using the loop variable.
  • The block must close with }, not ].

Discover other journeys with this module

What students will learn in this module

Every question in Module 34

Quick questions

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

Key terms

Students type the meaning of:

  • for...in

Match the pairs

for (let key in person) { ... }Match it to what it does
console.log(person[key]);Match it to what it does
Loops over each property name (key) of an objectMatch it to what it does
Retrieves a value using the loop's key variableMatch it to what it does
for (let key of obj) { ... }Match it to what it does
console.log(obj.key);Match it to what it does

Fill in the blanks

  1. ___ (let key in person) {

    Hint: Type the keyword that starts a for loop.

  2. for (let key __ person) {

    Hint: Type the keyword that connects the variable to the object's keys.

  3. for (___ color in car) {

    Hint: Type the keyword used to declare the loop variable.

  4. ___ (let prop in obj) {

    Hint: Type the keyword that starts a for loop.

  5. for (let prop __ obj) {

    Hint: Type the keyword that connects the variable to the object's keys.

Spot the error

Question 1

AFor (let k in a) {Blet a = {x: 1};Cconsole.log(k);D}

Question 2

Alet b = {y: 2};Bfor (let k of b) {Cconsole.log(k);D}

Question 3

Alet c = {z: 3};Bfor (let k in c) {Cconsole.log(c.k);D}

Question 4

Alet d = {a: 1};Bfor (let k in d) {Cconsole.log(k);D]

Question 5

Alet e = {b: 2};Bfor (let k in e) {Cconsole.log(k);Dconsole.log(e[k]);E]