- Home
- /
- Tutorials
- /
- JavaScript Tutorial
- /
- JavaScript do while Loop
JavaScript Loops
JavaScript do while Loop
The JavaScript do while loop runs a block of code at least once, then repeats it while a condition is true.
The JavaScript
do whileloop is used to run a block of code at least once, and then repeat it as long as a condition is true.
It is similar to the while loop, but there is one important difference.
A do while loop checks the condition after running the code block.
Example
Example
javascript
let i = 1;
do {
console.log(i);
i++;
} while (i <= 5);In this example, the loop runs from 1 to 5.
What is a do while Loop?
A do while loop first runs the code inside the do block.
After that, it checks the condition written after while.
If the condition is true, the loop runs again.
If the condition is false, the loop stops.
Syntax
Syntax
javascript
do {
// code to repeat
} while (condition);The code block is written after the do keyword.
The condition is written after the while keyword.
Simple do while Loop Example
Simple do while Loop Example
javascript
let count = 1;
do {
console.log("Hello JavaScript");
count++;
} while (count <= 3);Here, the message runs three times.
The value of count increases after every loop run.
do while Runs At Least Once
The main feature of a do while loop is that it always runs at least once.
Example
do while Runs At Least Once
javascript
let number = 10;
do {
console.log("This code runs once.");
number++;
} while (number <= 5);Here, the condition number <= 5 is false.
But the code still runs once because the condition is checked after the code block.
Difference Between while and do while
The while loop checks the condition before running the code.
The do while loop checks the condition after running the code.
Example with while:
Difference Between while and do while
javascript
let i = 10;
while (i <= 5) {
console.log(i);
i++;
}This code does not run because the condition is false from the beginning.
Example with do while:
Difference Between while and do while
javascript
let i = 10;
do {
console.log(i);
i++;
} while (i <= 5);This code runs once, even though the condition is false.
Counting Numbers with do while
You can use a do while loop to count numbers.
Example
Counting Numbers with do while
javascript
let i = 1;
do {
console.log(i);
i++;
} while (i <= 5);This prints numbers from 1 to 5.
Counting Backward
You can also count backward using a do while loop.
Example
Counting Backward
javascript
let i = 5;
do {
console.log(i);
i--;
} while (i >= 1);This prints numbers from 5 to 1.
do while with Arrays
A do while loop can also be used with arrays.
Example
do while with Arrays
javascript
let fruits = ["Apple", "Banana", "Mango"];
let i = 0;
do {
console.log(fruits[i]);
i++;
} while (i < fruits.length);This prints all items from the array.
Updating the Loop Variable
In a do while loop, you must update the loop variable manually.
Example
Updating the Loop Variable
javascript
let i = 1;
do {
console.log(i);
i++;
} while (i <= 5);Here, i++ increases the value of i after every loop run.
Without a proper update, the loop may never stop.
Complete Example
Complete Example
html
<!DOCTYPE html>
<html>
<head>
<title>JavaScript do while Loop</title>
</head>
<body>
<h1>JavaScript do while Loop</h1>
<p id="result"></p>
<script>
let i = 1;
let result = "";
do {
result += "Number: " + i + "<br>";
i++;
} while (i <= 5);
document.getElementById("result").innerHTML = result;
</script>
</body>
</html>Try It Yourself
Run the above example in the Try It Editor.
Try changing the value of i.
Example
Try It Yourself
javascript
let i = 10;Then check whether the loop still runs once.
Also try changing the condition:
Try It Yourself
javascript
while (i <= 10);Important Points
- The
do whileloop repeats code while a condition is true. - The code block runs before the condition is checked.
- A
do whileloop always runs at least once. - The condition is written after the
whilekeyword. - You must update the loop variable manually.
- Without a proper update, the loop can run forever.
- Use
do whilewhen the code should run at least one time.
Conclusion
The JavaScript do while loop is useful when you want to run code at least once before checking a condition.
It is similar to the while loop, but the condition is checked after the code block runs.
This makes do while useful for tasks where one execution is required before deciding whether to repeat the process.
