
Three racers, three strategies—who takes the lead in your code?
Imagine you're at a race track, watching three runners prepare for a big challenge. But here’s the twist: each of them has a different running style, just like JavaScript loops handle tasks in their own unique way.
Some are cautious, some are precise, and some are fearless. But which one should you choose for your next coding challenge?
Let's meet the racers! 🏁
🏃♂️ Racer 1: The While Loop
🔍 Thinks before taking every step!
The While Loop is like a runner who won’t start until everything is just right. They check their shoes, tie their laces, and make sure they’re ready before stepping onto the track. If the conditions aren’t perfect, they won’t move at all!
Syntax:
let steps = 0;
while (steps < 5)
{
console.log(`Step ${steps}`);
steps++;
}
✅ Best for: When you're not sure how many times something needs to happen, but you know the stopping condition.
Example:
Imagine you're waiting for a traffic light to turn green before crossing the road.
let light = "red";
while (light !== "green") {
console.log("Waiting at the crosswalk...");
// Simulating light change
light = Math.random() > 0.7 ? "green" : "red";
}
console.log("Go! The light is green!");
🚨 The Catch: If the condition is false from the start, the While Loop won’t run at all!
🏃♂️ Racer 2: The For Loop
📋 Knows exactly how many steps to take before starting!
The For Loop is like a sprinter who has planned everything in advance. They know how many laps they’ll run, when they’ll take breaks, and exactly when they’ll stop. There’s no hesitation!
Syntax:
for (let steps = 0; steps < 5; steps++)
{
console.log(`Step ${steps}`);
}
✅ Best for: When you know exactly how many times something should repeat.
Example:
Think of a baker making a batch of 10 cookies. They repeat the same process exactly 10 times—no more, no less!
for (let i = 1; i <= 10; i++)
{
console.log(`Baking cookie #${i}`);
}
console.log("All cookies are ready!");
🚨 The Catch: If you don’t know the exact number of repetitions beforehand, this might not be the best loop.
🏃♂️ Racer 3: The Do-While Loop
💨 Takes the first step no matter what!
The Do-While Loop is the risk-taker of the race. Unlike the others, this racer starts running first and checks the conditions later. Even if the race should never have started, they’ve already taken one step!
Syntax:
let steps = 0;
do {
console.log(`Step ${steps}`);
steps++;
} while (steps < 5);
✅ Best for: When you must run the code at least once, no matter what.
Example:
Think of a kid who always takes a bite of ice cream before realizing if it’s too cold!
let tooCold;
do {
console.log("Taking a bite of ice cream...");
tooCold = Math.random() > 0.6; // 40% chance it's too cold
} while (!tooCold);
console.log("Brrr! That’s freezing! ❄️");
🚨 The Catch: No matter what, this loop always runs at least once—even if it wasn’t needed!
🏆 Which Racer Should You Choose?
Each loop is great at different tasks:
✅ While Loop – Best for uncertain repetitions (like waiting for user input).
✅ For Loop – Best when you know exactly how many times something should happen.
✅ Do-While Loop – Best when you must execute the code at least once (like showing a message before validation).
Ask Yourself:
🔹 Do I know how many repetitions I need? → Use For Loop
🔹 Do I need to wait for a condition? → Use While Loop
🔹 Must the code run at least once? → Use Do-While Loop
With the right loop, you’ll be coding like a pro!
About the Creator
Sreya Satheesh
Senior Software Engineer | Student
https://github.com/sreya-satheesh
https://leetcode.com/u/sreya_satheesh/
Reader insights
Outstanding
Excellent work. Looking forward to reading more!
Top insights
Compelling and original writing
Creative use of language & vocab
Easy to read and follow
Well-structured & engaging content
Excellent storytelling
Original narrative & well developed characters
Masterful proofreading
Zero grammar & spelling mistakes
On-point and relevant
Writing reflected the title & theme


Comments