Decisions & Loops Lesson 14 of 30
JavaScript for Loops
Repeat code with for loops — count up and down, skip by steps, build multiplication tables, loop through strings and arrays, and use break and continue.
Imagine writing "Hindi na ako mag-iingay sa klase" 100 times. A computer can do it in a blink with a loop. Loops repeat a block of code, and they're one of the most powerful ideas in programming.
The for loop
for (start; condition; step) {
// code to repeat
}
<pre id="out"></pre>
<script>
let text = "";
for (let i = 1; i <= 5; i++) {
text += "Count: " + i + "\n";
}
document.getElementById("out").textContent = text;
</script>
Read the first line as three parts:
- Start
let i = 1: create a counteristarting at 1. This runs once. - Condition
i <= 5: before each round, check this. If false, the loop ends. - Step
i++: after each round, add 1 toi.
So the loop runs with i = 1, 2, 3, 4, 5, then i becomes 6, the condition is false, and it stops. Each round is called an iteration.
Counting in different ways
for (let i = 10; i >= 1; i--) { } // 10, 9, 8 … 1 (countdown)
for (let i = 0; i <= 100; i += 10) { } // 0, 10, 20 … 100
for (let i = 2; i <= 20; i += 2) { } // even numbers 2 to 20
Multiplication table
<pre id="out"></pre>
<script>
const n = 7;
let table = "";
for (let i = 1; i <= 10; i++) {
table += `${n} × ${i} = ${n * i}\n`;
}
document.getElementById("out").textContent = table;
</script>
Change n to any number and you get a new table instantly.
Adding things up
A common pattern: start a total at 0, then add to it inside the loop.
<p id="out"></p>
<script>
let sum = 0;
for (let i = 1; i <= 100; i++) {
sum += i;
}
document.getElementById("out").textContent = "1 + 2 + … + 100 = " + sum;
</script>
Looping through an array
Arrays start at index 0, so the classic loop starts at 0 and runs while i < array.length:
<ul id="list"></ul>
<script>
const sections = ["Rizal", "Bonifacio", "Mabini", "Luna"];
let html = "";
for (let i = 0; i < sections.length; i++) {
html += `<li>${i + 1}. Grade 10 – ${sections[i]}</li>`;
}
document.getElementById("list").innerHTML = html;
</script>
for…of: the easy way
When you don't need the index number, for...of is simpler. It gives you each item directly:
for (const section of sections) {
console.log(section);
}
It also works with strings, giving you one character at a time: for (const letter of "Luzon") { ... }.
break and continue
breakstops the whole loop immediately.continueskips the rest of this round and goes to the next one.
<pre id="out"></pre>
<script>
const grades = [88, 92, 0, 71, 95];
let log = "";
for (const g of grades) {
if (g === 0) {
log += "Skipping a missing grade\n";
continue;
}
if (g < 75) {
log += `Found a failing grade: ${g}. Stopping.\n`;
break;
}
log += `${g} is OK\n`;
}
document.getElementById("out").textContent = log;
</script>
Loops inside loops
<pre id="out"></pre>
<script>
let rows = "";
for (let r = 1; r <= 5; r++) {
let line = "";
for (let c = 1; c <= r; c++) {
line += "⭐";
}
rows += line + "\n";
}
document.getElementById("out").textContent = rows;
</script>
The outer loop makes each row, and the inner loop fills the row with r stars. Nested loops are the basis for grids, seat plans and game boards.
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04