JavaScript do while Loop

In the previous lesson, we learned how a while loop checks its condition before running the body. Now let’s meet its close cousin, the do…while loop, which flips that order and runs the body first.

🔁 What is a do…while Loop?

A do…while loop runs its body once, and then checks the condition to decide whether to run again. Because the check happens at the end, the body always runs at least one time, even when the condition is false from the very start.

Here is the syntax:

do-while-loop.js
do {
// code to run
} while (condition);

Let’s walk through the pieces:

  • do { ... } holds the body, the code we want to run on each pass.
  • while (condition) sits at the bottom and is checked after the body runs.
  • As long as the condition stays true, the loop jumps back to do and runs the body again.

Mind the semicolon

A do…while loop ends with a semicolon after while (condition). This is the one loop in JavaScript that requires it.

🧮 A Simple Example

Let’s count from one to five. We start at 1, print the value, add one each time, and keep going while count is still less than or equal to 5.

do-while-loop.js
let count = 1;
do {
console.log(count); // 1, 2, 3, 4, 5
count++;
} while (count <= 5);

Here is how this runs line by line:

  • let count = 1; sets up the starting value before the loop begins.
  • console.log(count); prints the current value, so 1 appears before any check happens.
  • count++; increases count by one on every pass.
  • while (count <= 5) checks the condition at the bottom; once count reaches 6 the condition is false and the loop stops.

⚖️ do…while vs while

The key difference is when the condition is checked. A while loop checks first, so the body might never run. A do…while loop checks last, so the body always runs at least once.

Feature while loop do…while loop
When the condition is checked Before the body After the body
Minimum times the body runs 0 1
If the condition starts false Body is skipped Body runs once
Ends with a semicolon No Yes

This next pair makes the difference clear. Both loops have a condition that is false from the start, yet they behave differently.

do-while-loop.js
// while: condition is false, so the body never runs
let a = 10;
while (a < 5) {
console.log(a); // never prints
a++;
}
// do...while: the body runs once before the check
let b = 10;
do {
console.log(b); // 10
b++;
} while (b < 5);

Let’s compare the two halves:

  • In the while loop, a starts at 10, so a < 5 is false right away and the body is skipped, printing nothing.
  • In the do…while loop, b also starts at 10, but the body runs before the check, so 10 prints once.
  • After that single pass, b < 5 is false and the do…while loop stops too.

When to reach for do...while

Use a do…while loop when the body must run at least once, such as showing a menu or asking the user a question before deciding whether to repeat.

🙋 A Practical Example

A do…while loop is perfect when we need to ask for input at least once. Here we keep asking Alex for a password until the correct one is typed. The prompt has to appear at least once, which is exactly what this loop guarantees.

do-while-loop.js
let password;
do {
password = prompt("Enter the password:");
} while (password !== "open123");
console.log("Welcome, Alex!");

Here is what each part does:

  • let password; declares the variable before the loop so we can use it both inside and after.
  • password = prompt("Enter the password:"); runs first and always asks at least once.
  • while (password !== "open123") keeps the loop going while the answer is wrong.
  • The moment the right value is entered, the condition is false, the loop ends, and console.log prints the welcome message.

⚠️ Common Mistakes to Avoid

Mistake Problem Solution
Forgetting the semicolon after while (...) JavaScript throws a syntax error Always end with } while (condition);
Never changing the condition variable The condition stays true and the loop runs forever Update a value inside the body so the loop can end
Expecting the body to skip when the condition starts false The body still runs once Use a while loop when the body may need to run zero times

The most common trap is the infinite loop. The example below never changes count, so the condition is always true and the loop never stops.

do-while-loop.js
let count = 1;
// ❌ Wrong: count never changes, so this runs forever
do {
console.log(count);
} while (count <= 5);
// ✅ Correct: count grows until the condition becomes false
do {
console.log(count);
count++;
} while (count <= 5);

Here is the difference between the two blocks:

  • In the wrong version, the body only prints count and never updates it, so count <= 5 stays true forever.
  • In the correct version, count++ increases the value on each pass.
  • Because count keeps growing, it eventually reaches 6, the condition turns false, and the loop ends.

🔧 Try It Yourself!

  1. Write a do…while loop that prints the numbers from 1 to 5.
  2. Set a variable to 100, then write a do…while loop with the condition value < 10 and watch the body still run once.
  3. Build the same count using a while loop and compare how the two behave when the condition starts false.
  4. Use a do…while loop to ask the user a question until they type the answer you expect.

🧩 What You’ve Learned

  • ✅ A do…while loop runs its body first, then checks the condition
  • ✅ The body always runs at least once, even if the condition starts false
  • ✅ The syntax is do { ... } while (condition); and ends with a semicolon
  • ✅ A do…while loop is ideal for asking for input at least once
  • ✅ Always update a value inside the body to avoid an infinite loop

🚀 What’s Next?

Now that you know every kind of loop, let’s learn how to control them from the inside. Let’s continue to break and continue.

Share & Connect