Skip to content

Chapter 6 of 33

Loops

Use for, while, do-while, for...in, for...of, break, and continue safely.

30 minutes 10 quick checksBy Subha Prasad
Lesson 6 of 33Course navigation

Lesson content

Read, practise, then check your understanding

Use classic for for indexed counting, while for condition-driven repetition, and do...while when the body must run once.

Values versus keys

const values = [2, 4, 6];
for (const value of values) {
  console.log(value);
}

const record = { name: "Mira", score: 90 };
for (const key of Object.keys(record)) {
  console.log(key, record[key]);
}

for...of consumes iterable values. for...in enumerates enumerable string keys, including inherited ones, so avoid it for arrays and guard object ownership when needed. break exits and continue advances. Verify termination and complexity; array methods often express transformations more clearly than manual loops.

Knowledge check

Answer every question correctly to complete this chapter.

Which statement best describes for loop?
Which JavaScript term matches this description: A loop with initialization, condition, and update clauses.
Which statement best describes while loop?
Which JavaScript term matches this description: A loop checking its condition before each iteration.
Which statement best describes do-while?
Which JavaScript term matches this description: A loop whose body executes at least once.
Which statement best describes for...of?
Which JavaScript term matches this description: Iteration over values produced by an iterable.
Which statement best describes for...in?
Which JavaScript term matches this description: Enumeration of an object's enumerable string property keys.

0 of 10 checks passed

Your progress is saved on this device.