⬅ Previous Topic
JavaScript While LoopNext Topic ⮕
JavaScript Functionsdo...while
Loop⬅ Previous Topic
JavaScript While LoopNext Topic ⮕
JavaScript Functionsdo...while
Loop in JavaScriptThe do...while
loop is one of JavaScript's control flow statements that ensures the loop body executes at least once — no matter what. If you've ever needed to run a block of code before checking a condition, this loop is your go-to.
do...while
do {
// code to be executed
} while (condition);
Unlike a while
loop that checks the condition first, the do...while
loop executes the block once before checking the condition. It’s a perfect fit for scenarios where an action must occur at least once — like prompting user input, menu systems, or retry logic.
let count = 1;
do {
console.log("Count is: " + count);
count++;
} while (count <= 3);
Count is: 1
Count is: 2
Count is: 3
Here, the loop starts with count = 1
. It prints the value, then increments it. This continues until count
exceeds 3. Since the check comes after the execution, even if count
was more than 3 initially, the message would still print once.
let count = 5;
do {
console.log("This will run once even though count > 3");
} while (count <= 3);
This will run once even though count > 3
This showcases the beauty — or danger — of do...while
. It doesn’t care if the condition fails; the code block will always execute once.
let input;
do {
input = prompt("Enter a number greater than 10:");
} while (parseInt(input) <= 10);
console.log("You entered: " + input);
This code ensures the user enters a number greater than 10 — and keeps prompting until they do. The loop is helpful because the prompt must happen at least once regardless of the input.
break
and continue
with do...while
let i = 0;
do {
if (i === 3) {
break;
}
console.log("i is: " + i);
i++;
} while (i < 5);
i is: 0
i is: 1
i is: 2
As soon as i
hits 3, the loop is broken using break
.
continue
let i = 0;
do {
i++;
if (i === 2) {
continue;
}
console.log("i is: " + i);
} while (i < 4);
i is: 1
i is: 3
i is: 4
When i
equals 2, continue
skips that iteration — the print is skipped.
do...while
const colors = ["red", "green", "blue"];
let index = 0;
do {
console.log("Color:", colors[index]);
index++;
} while (index < colors.length);
Color: red
Color: green
Color: blue
This is a basic array iteration. While not the most common method (we usually use for
or forEach
), it demonstrates that do...while
is flexible.
let num = 1;
do {
console.log(num);
// forgot to increment num
} while (num < 5);
This creates an infinite loop because the condition num < 5
is always true and num
never changes. Always ensure your loop will reach a stopping condition!
do...while
do...while
executes at least once before checking the condition.Try writing a program that keeps asking the user for a correct password until they enter it correctly — use do...while
and alert the user each time.
⬅ Previous Topic
JavaScript While LoopNext Topic ⮕
JavaScript FunctionsYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.