Objects – for...in Loop, online
Six activities your students do on their own devices. See who's finished, and their scores.
Learning
Understanding
Testing
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 ].
PREVIOUS · MODULE 33Objects – Methods (keys, values, entries)
YOU ARE HERE · MODULE 34Objects – for...in Loop
NEXT · MODULE 35Nested Arrays and Objects
What students will learn in this module
Every question in Module 34
Quick questions
- Does for...in loop over the keys of an object?AYesBNo
- Is 'of' used instead of 'in' for looping over an object's keys?AYesBNo
- Must the loop variable be declared with let before 'in'?ANoBYes
- Does obj[key] access the value for the current key in the loop?AYesBNo
- 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 doesconsole.log(person[key]);Match it to what it doesLoops over each property name (key) of an objectMatch it to what it doesRetrieves a value using the loop's key variableMatch it to what it doesfor (let key of obj) { ... }Match it to what it doesconsole.log(obj.key);Match it to what it doesFill in the blanks
___ (let key in person) {Hint: Type the keyword that starts a for loop.
for (let key __ person) {Hint: Type the keyword that connects the variable to the object's keys.
for (___ color in car) {Hint: Type the keyword used to declare the loop variable.
___ (let prop in obj) {Hint: Type the keyword that starts a for loop.
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]
