for...of Loops

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

for...of Loops

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Does for...of loop through each value in an array?
A Yes
B No
2. Does for...of require the keyword 'of'?
A Yes
B No
3. Is 'in' the same as 'of' in this kind of loop?
A No
B Yes
4. Does for...of need a variable name to hold each value?
A No
B Yes
5. Must 'for' be written in lowercase?
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 fruit of fruits) { ... } AGives direct access to each value in nums, not the index
2for (let n of nums) { ... } Blet fruit
3Loops directly over each value in an array CWrong - use 'of', not 'in', to loop through values
4Holds the current value during each pass of the loop Dfor...of
5for (let n in nums) { ... } EWrong - 'for' must be lowercase
6For (let n of nums) { ... } FLoops through each value in the fruits array

Definitions

for...of

Fill in the Blanks

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

1.
___ (let fruit of fruits) {
2.
for (let fruit __ fruits) {
3.
___ (let color of colors) {
4.
for (___ item of items) {
5.
for (let num of nums) _

Spot the Error

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

Question 1
A Fr (let x of arr) {
B console.log(x);
C }
Question 2
A let arr = [1,2,3];
B for (let x in arr) {
C console.log(x);
D }
Question 3
A let list = [1,2,3];
B for (let v of list) {
C conssole.log(v);
D }
Question 4
A let nums = [1,2,3];
B for (let n of nums) {
C console.log(n);
D ]
Question 5
A let a = [1,2];
B for (let x of a) {
C console.log(x);
D console.log("done");
E ]

Answer Key

for...of Loops
Section A: Quick Questions
Q1
A — Yes
Q2
A — Yes
Q3
A — No
Q4
B — Yes
Q5
B — Yes
Section B: Match the Pairs
Q1
for (let fruit of fruits) { ... } → Loops through each value in the fruits array
Q2
for (let n of nums) { ... } → Gives direct access to each value in nums, not the index
Q3
Loops directly over each value in an array → for...of
Q4
Holds the current value during each pass of the loop → let fruit
Q5
for (let n in nums) { ... } → Wrong - use 'of', not 'in', to loop through values
Q6
For (let n of nums) { ... } → Wrong - 'for' must be lowercase
Section C: Definitions
Q1
for...of — Loops directly over each value in an array, without needing an index.
Section D: Fill in the Blanks
Q1
for (let fruit of fruits) {
Q2
for (let fruit of fruits) {
Q3
for (let color of colors) {
Q4
for (let item of items) {
Q5
for (let num of nums) {
Section E: Spot the Error
Q1
A — 'for' is misspelled as 'Fr'.
Q2
B — Use 'of', not 'in', to loop through array values.
Q3
C — 'console' is misspelled as 'conssole'.
Q4
D — The block must close with }, not ].
Q5
E — The block must close with }, not ].