Decisions & Loops Lesson 15 of 30
JavaScript while and do…while Loops
Repeat code while a condition is true with while and do…while loops — savings goals, guessing games — and learn how to avoid infinite loops.
A for loop is best when you know how many times to repeat. But sometimes you don't know: "keep saving until I have ₱1,000", "keep asking until the answer is right". For these, use a while loop.
The while loop
while (condition) {
// repeat while the condition is true
}
<pre id="out"></pre>
<script>
const goal = 1000;
const perWeek = 150;
let savings = 0;
let weeks = 0;
let log = "";
while (savings < goal) {
savings += perWeek;
weeks++;
log += `Week ${weeks}: ₱${savings}\n`;
}
log += `\nIt takes ${weeks} weeks to reach ₱${goal}.`;
document.getElementById("out").textContent = log;
</script>
We didn't know in advance it would take 7 weeks; the loop just kept going until the condition savings < goal became false. Try changing perWeek to 80.
Infinite loops
If the condition never becomes false, the loop runs forever and freezes the page. This happens when you forget to change the variable inside the loop:
let i = 1;
while (i <= 5) {
console.log(i);
// forgot i++ → i is always 1 → runs forever!
}
do…while: run at least once
A do...while loop checks the condition at the end, so the code always runs at least once:
do {
// runs once, then repeats while the condition is true
} while (condition);
<button onclick="play()">Roll until 6 🎲</button>
<pre id="out"></pre>
<script>
function play() {
let roll;
let rolls = [];
do {
roll = Math.floor(Math.random() * 6) + 1;
rolls.push(roll);
} while (roll !== 6);
document.getElementById("out").textContent =
"Rolls: " + rolls.join(", ") + "\nGot a 6 after " + rolls.length + " roll(s)!";
}
</script>
We have to roll at least once before we can check if it's a 6, so do...while fits perfectly.
Digits of a number
A classic exercise: count the digits of a number by dividing by 10 until it reaches 0.
<p id="out"></p>
<script>
let n = 2026;
const original = n;
let digits = 0;
while (n > 0) {
n = Math.floor(n / 10);
digits++;
}
document.getElementById("out").textContent = `${original} has ${digits} digits.`;
</script>
A guessing game
<input id="g" type="number" min="1" max="20" style="width:70px">
<button onclick="guess()">Guess</button>
<p id="out">I'm thinking of a number from 1 to 20…</p>
<script>
const secret = Math.floor(Math.random() * 20) + 1;
let tries = 0;
function guess() {
const g = Number(document.getElementById("g").value);
tries++;
let msg;
if (g === secret) msg = `🎉 Tama! It was ${secret}. You needed ${tries} tries.`;
else if (g < secret) msg = "Higher! ⬆️";
else msg = "Lower! ⬇️";
document.getElementById("out").textContent = msg;
}
</script>
Here, each click is one "round", and the game continues until you guess correctly. On web pages, we usually wait for events like clicks instead of using a blocking while loop, because a while loop that waits for the user would freeze the page.
Which loop should I use?
| Loop | Best when… |
|---|---|
for | You know the number of repetitions (1 to 10, every array item) |
for...of | You want every item of an array or string |
while | You repeat until something happens |
do...while | Same, but the code must run at least once |
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04