These questions ask how many times a loop runs, where control moves after break or continue, and whether an iteration variable receives values, indexes, or property names. Trace the loop form before looking at the data.
Choose a control-flow or iteration trace
| Construct | Trace to use |
|---|---|
| for statement | Run initialization once, then check the condition before each body execution. After a normally completed body, run the update and check again. For an array with length n, starting at zero and continuing while the index is less than n visits every valid index once. |
| while and do...while | while checks before the body, so it can run zero times. do...while runs the body first and checks afterward, so the first body execution does not depend on the condition. |
| break and continue | An unlabeled break exits the nearest loop immediately. Continue skips the rest of the current body; in a for statement, control then reaches the update before the next condition check. |
| for...of | Consume values from an iterable. With an array, the variable receives elements. With a string, it advances by Unicode code point, so one valid surrogate pair is consumed together. |
| for...in | Enumerate enumerable string property names, including eligible inherited ones. On an ordinary array these include index strings rather than the element values. |
When both an array index and its value are needed, array.entries() provides an iterable that yields one two-part pair at a time; pair destructuring can bind the two parts during for...of. An ordinary object does not provide the iterable protocol by default, so direct use with for...of throws a TypeError. Object.keys, Object.values, or Object.entries can first produce an iterable Array for the desired view.
Where the mistakes come from
| Tempting route | How to reject it |
|---|---|
| Reordering the for phases, making while run once, or treating break as a final update | Write the trace explicitly. Initialization is once; pre-test loops check before entry; an executed break leaves before later work in that loop. |
| Using continue as another spelling of break | Continue abandons only the remaining statements of the current iteration. The loop's own next-iteration sequence still determines what happens before the next test. |
| Swapping for...of values with for...in property names | Ask whether the source is being consumed as an iterable or enumerated as an object's properties. On arrays, one route gives elements and the other gives index strings. |
| Counting UTF-16 units as separate string iterations or allowing an index equal to length | String iteration advances by code point, while array index loops use a half-open boundary. The value equal to length is already beyond the final valid index. |
Try it
Which statements about a do...while loop are true? Select all that apply.
- Its body runs at least once
- It tests its condition before the initial body execution
- Its condition controls whether another iteration begins
- Its condition is ignored after the body runs
A for...of loop processes the array ["red", "blue"]. What does its loop variable receive in order?
- The strings 0 and 1
- Two key-value pair arrays
- The array length twice
- The strings red and blue
An unlabeled break executes inside the inner loop of two nested loops. What is its immediate control-flow effect?
- Only the rest of the current iteration is skipped
- Both loops always terminate
- The outer loop terminates while the inner loop continues
- The inner loop terminates