Loops in JavaScript: Code That Doesn’t Get Tired

Ever wished your code could repeat itself without copy-pasting a hundred times? That’s where loops come in. They’re like your favorite playlist on repeat — but smarter!
🔹 for Loop
📦 Analogy: "I will watch 3 episodes of a show before sleeping."
🧠 Use when you know how many times to run.
for (let i = 1; i <= 3; i++) {
console.log("Watching episode " + i);
}
while Loop
🚦 Analogy: “Tossing a coin until you get heads less than 3 times.”
🧠 Use when you don’t know exactly how long it’ll run.
let headsCount = 0;
while (headsCount < 3) {
console.log("Tossing the coin..."+ headsCount);
headsCount++;
}
do...while Loop
🍩 Analogy: You're at a donut shop and must eat at least one donut. You’ll keep eating while you’ve eaten less than 4.
🧠 Executes at least once, no matter what.
let donutsEaten = 0;
do {
console.log(`Donut #${donutsEaten + 1} eaten 🍩`);
donutsEaten++;
} while (donutsEaten < 4);
Wrap-Up
Loops help you run the same block of code multiple times with different values. They're perfect for working with data, making decisions, and keeping your code short and smart. 💡🔄
Written by Akum Imchen | Mentored and guided by Devsync