JavaScript do…while Loop

Introduction

The do…while loop is slightly different from other loops because it executes the code block at least once, regardless of whether the condition is true or false.

This makes it especially useful in situations where you want the code to run first and then check the condition afterward—such as user input validation, menu-driven programs, or retry logic.

In this guide, you’ll learn everything about the JavaScript do…while loop, including syntax, working, examples, real-life use cases, common mistakes, and interview questions.

What is a do…while Loop?

A do…while loop is a control structure that executes a block of code once first, and then repeatedly executes it as long as a specified condition is true.

A do-while loop runs at least once because it is checked at the end of the iteration.

📖
Best Practices:
  • Use meaningful conditions.
  • Avoid infinite loops.
  • Use break carefully.

Syntax of do…while Loop


do {
   // code to execute
} while (condition);

Basic Example


let i = 1;

do {
   console.log(i);
   i++;
} while (i <= 5);

Output:

1
2
3
4
5

How do...while Loop Works

  1. Code block executes first
  2. The condition is checked
  3. If true → loop repeats
  4. If false → loop stops

Flow Example


let i = 0;

do {
   console.log(i);
   i++;
} while (i < 3);

Execution:

  • i = 0 → print 0
  • i = 1 → print 1
  • i = 2 → print 2
  • i = 3 → stop

while vs do...while

Feature while do...while
Condition Check Before execution After execution
Execution 0 or more times At least once
Syntax while(condition) {
  // code
}
do {
  // code
} while(condition);
Example let i = 1;
while(i <= 3){
  console.log(i);
  i++;
}
let i = 1;
do {
  console.log(i);
  i++;
} while(i <= 3);

Real-Life Examples: Menu System


let choice;

do {
   console.log("1. Add");
   console.log("2. Delete");
   console.log("3. Exit");

   choice = prompt("Enter choice:");
} while (choice !== "3");

Output:

1. Add
2. Delete
3. Exit
Enter choice:

2. Countdown Timer


let count = 5;

do {
   console.log(count);
   count--;
} while (count > 0);

Output:

5
4
3
2
1

Break and Continue Statement in do...while Loop

Break Statement in do...while Loop

It stops the loop immediately.

Example:


let i = 1;

do {
   if (i === 3) break;
   console.log(i);
   i++;
} while (i <= 5);

Output:

1
2

Continue Statement in do...while Loop

It is used to skip the current iteration.


let i = 0;

do {
   i++;
   if (i === 3) continue;
   console.log(i);

} while (i <= 5);

Output:

1
2
4
5
6

When to Use do...while Loop

You should use do...while Loop when

  1. Code must execute at least once
  2. Taking user input
  3. Menu-driven programs
  4. Retry operations
  5. Validation scenarios

When NOT to Use

You should avoid when:

  1. The condition should be checked first.
  2. Execution depends strictly on the condition.
  3. No guaranteed first run is needed.

Common Mistakes

You will see a common mistake when using a do...while Loop

1. Infinite Loop

When the developer missed i++ in the code.


let i = 1;

do {
   console.log(i);
} while (i <= 5); // missing i++

2. Forgetting Semicolon

When the developer missed a semicolon in the code.


do {
   console.log("Test");
} while (true) // missing ;

JavaScript do-while loop – Interview Questions

Q 1: What is a do...while loop?
Ans: Executes code at least once.
Q 2: Is it exit-controlled?
Ans: Yes.
Q 3: Condition checked when?
Ans: After loop execution.
Q 4: Use case?
Ans: Menu-driven programs.
Q 5: Can it be infinite?
Ans: Yes.

JavaScript do-while loop – Objective Questions (MCQs)

Q1. The do...while loop executes the code block at least ______.






Q2. The condition in do...while is checked ______.






Q3. Which loop guarantees at least one execution?






Q4. The do...while loop ends with a ______.






Q5. do...while is useful when the loop body must run ______.






Conclusion

The JavaScript do...while loop is a powerful control structure that ensures a block of code runs at least once before evaluating the condition. It is particularly useful for scenarios like user input validation, retry mechanisms, and menu-driven programs.