</>
Skip to content
JavaScript lessons (33/64)

JavaScript — Loops

Why loops exist

Print numbers 1–5 without a loop:

console.log(1); console.log(2); console.log(3); console.log(4); console.log(5);

Now imagine 1–5000. Loops repeat a block as many times as needed:

for — the counting classic

for (let i = 1; i <= 5; i++) {
    console.log(i);
}

Three clauses, read once slowly:

for ( let i = 1 ;  i <= 5 ;  i++ )
      ──┬──────   ───┬───   ─┬─
     start      keep going?  step after each pass
  1. let i = 1 — runs once before everything
  2. i <= 5 — checked before every iteration; false ends the loop
  3. i++ — runs after each iteration

What you see

1
2
3
4
5

Counting down, skipping, stepping by 10 — all just clause edits:

for (let i = 10; i >= 0; i -= 2) { … }   // 10,8,6,4,2,0

for...of — visiting each item

const fruits = ["apple", "banana", "mango"];

for (const fruit of fruits) {
    console.log(fruit);
}

No counters, no off-by-one risk. The default choice for arrays/strings:

for (const ch of "abc") console.log(ch);   // a b c

while — repeat until condition dies

let fuel = 3;

while (fuel > 0) {
    console.log("Vroom");
    fuel--;
}

Use when you don't know the count upfront — waiting for user input, draining a queue.

The infinite-loop hazard: forgetting to change the condition freezes the tab:

while (true) { }        // browser locks. Don't.

Always ensure something inside moves toward falseness.

do...while — check AFTER

let answer;
do {
    answer = prompt("Type 'go'");     // body runs at least ONCE
} while (answer !== "go");

Only difference from while: condition tested after the first pass.

Breaking early & skipping

for (const n of [1, 7, 0, 9]) {
    if (n === 0) continue;    // skip just this iteration
    if (n > 8) break;         // abandon whole loop
    console.log(n);
}
// prints 1, 7 then stops before 9

Choosing

SituationLoop
Known count / index mathfor (let i…)
Every item of array/stringfor...of
Unknown repetitionswhile
Must run at least oncedo...while

(Modern code often skips explicit loops entirely via map/filter/reduce — its own lesson.)

Common mistakes: < vs <= off-by-one; mutating the array while iterating it; infinite loops from forgotten increments.

Mini Practice

  1. Multiples of 7 up to 70 with a for
  2. Sum all numbers in an array with for...of
  3. Countdown 10→1 then log "Liftoff!"
  4. Number-guessing game loop with do...while + prompt
  5. Demo continue and break inside one loop; predict output first

Next: break → (next new topic: maps)

Related Topics

Frequently Asked Questions about Loops

What is Loops in JavaScript?

Loops is a fundamental concept in JavaScript. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Loops?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Loops.

Why is Loops important in JavaScript?

Loops is essential for JavaScript development. Understanding this concept will help you write better code and solve real-world problems more effectively.