JavaScript · Lesson 4 of 8
Loops
for, while, for…of and breaking out of loops.
Loops repeat code.
for (let i = 0; i < 5; i++) { … }— counted loops.while (condition) { … }— repeat while true; make sure it ends!for (const item of list) { … }— every item of an array or string.breakstops the loop;continueskips to the next round.
Try it yourself
Edit the code and press Run (or Ctrl/⌘ + Enter). Code runs only in your browser, in a sandbox.
Key points
- for for counting, for…of for lists.
- Every while loop needs an exit.
- break / continue control the flow.
Quick check
How many times does for (let i = 0; i < 3; i++) run?
i takes the values 0, 1 and 2 — three iterations.
Practice task
Use a loop to calculate the total of [3.5, 6, 8, 12] and print the average.
Interview corner
for…of vs for…in?
for…of iterates values of arrays and other iterables. for…in iterates property keys of an object — avoid it for arrays.
How do you stop a loop early?
Use break. continue skips just the current iteration.
Further reading
Official documentation, linked for deeper study. These are outside sites; this lesson's text is our own.
Original lesson written by the InfoparkDaily team · © 2026 InfoparkDaily · Free to learn — no paid course or subscription.