Lesson 7 of 28

In progress

Course progress

JavaScript

Loops: for, while and for…of

Repeat work with the right loop, iterate arrays and objects safely, and recognise the loop that never ends.

Loops are where a program stops being a list of instructions and starts doing work proportional to its data. JavaScript has several, and choosing the right one is mostly about what you are iterating.

What you will learn

  • Write for, while and for...of loops.
  • Iterate arrays and objects without off-by-one errors.
  • Break out of a loop and avoid infinite ones.

The classic for loop

JavaScript
Editable. Runs in your browser.





Press Run to see the output.

Three parts: initialise, test, update. All three are optional, which makes it possible to write a loop that never ends, so keep them visible on one line.

Use the classic form when the index matters, such as numbering output or accessing a parallel array. For everything else, for...of is clearer.

for…of

JavaScript
Editable. Runs in your browser.





Press Run to see the output.

for...of walks the values. entries() gives index and value pairs when you need both. This form cannot skip items or run past the end, which removes a whole class of bugs.

Do not use for...in on arrays. It iterates property names as strings, including inherited ones, and the order is not guaranteed in the way you expect. for...in is for object keys.

while and do…while

while repeats while a condition holds. Use it when the number of iterations is not known in advance.

JavaScript
Editable. Runs in your browser.





Press Run to see the output.

The body must change something the condition depends on, or the loop never ends. A while (true) is acceptable only with a clear break inside.

do...while checks its condition after the body, so the body runs at least once. It is rare, and useful for "ask, then repeat while invalid" flows.

break and continue

break exits the nearest loop. continue skips the rest of the current iteration.

JavaScript
Editable. Runs in your browser.





Press Run to see the output.

That prints 3 and 5. The even numbers are skipped, and the loop stops at 12. Both statements affect only the innermost loop; inside nested loops, a break does not escape the outer one. A labelled break can, but it is usually a sign the loop should be a function with a return.

Iterating objects

JavaScript
Editable. Runs in your browser.





Press Run to see the output.

Object.entries returns pairs, which destructure into name and value. Object.keys and Object.values are useful for mapping and summing. Property order follows insertion order for string keys, but do not build logic on that.

Accumulating results

The pattern is: start with an empty value, update it inside the loop, use it after.

JavaScript
Editable. Runs in your browser.





Press Run to see the output.

Printing inside the loop shows each step; printing after shows the result. Decide which one the reader needs. For totals, log once after the loop.

Loops versus array methods

map, filter and reduce express the same work without an index. They are covered in module 5, but the rule of thumb is:

  • Transforming every item → map.
  • Selecting some items → filter.
  • Producing one value → reduce.
  • Doing something for each item, such as logging → for...of or forEach.

A for loop is still right when you need to stop early, because map and filter always visit every item. forEach is the same: break inside it is a syntax error, so a search that can finish early needs a real loop.

for (let i = 0; i <= items.length; i++) reads one past the end and produces undefined on the last pass. Arrays are zero-indexed, so the condition is i < items.length. This is the most common off-by-one in JavaScript.

Given an array of scores, count how many pass 50, sum only the passing scores, and stop counting once you find a score above 95. Use one loop.

Show solution
JavaScript
Editable. Runs in your browser.





Press Run to see the output.

The break stops before the 96 is counted, and the continue skips the low score. Both conditions live in one pass, which is clearer than three separate loops.

Recap

  • for...of for values, entries() for index and value, classic for when the index is the point.
  • while when the count is unknown; always change the condition inside.
  • break and continue affect one loop; prefer small functions over labelled breaks.

Next, you will give repeated logic a name and a clear contract with functions.

Why does for...in give me indexes as strings?
It iterates property keys, and object keys are strings. It also includes inherited enumerable properties, which is why it is the wrong tool for arrays.
Can I loop over a NodeList?
Yes, with `for…of` in modern browsers, or with `forEach` on the result of `querySelectorAll`. Older code converts it with `Array.from`.

Progress is saved in this browser only. No account, no email.

Admin

About the author

Admin

Instructor and technical writer at tutorials.in

Writes beginner lessons for tutorials.in. Each lesson is checked against the language or tool documentation, and examples are run before publishing.

Reviewed by the editorial team before publishing. Last updated November 16, 2026.