HTML5ANDCSS3 logoHTML5ANDCSS3Tools and Tutorials
  • Insights
  • Verify Certificate
HTML5ANDCSS3 logoHTML5ANDCSS3Tools and Tutorials
HTML5ANDCSS3 logoHTML5ANDCSS3Tools and Tutorials

HTML5andCSS3.org helps developers learn modern frontend skills with practical tutorials, production-ready snippets, and fast web tools built for everyday coding.

Quick Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Copyright
  • Disclaimer

Top Tools

  • Try It Editor
  • JSON Formatter
  • CSS Minifier
  • HTML Minifier
  • Base64 Encoder / Decoder

Contact

  • business@html5andcss3.org
Newsletter

Learn at your own pace, practice with useful tools, and build websites you're proud of.

© 2011-2026 html5andcss3.org. All rights reserved.

How to Use

JavaScript Tutorial

JavaScript Introduction

What is JavaScriptJavaScript FeaturesJavaScript SyntaxHow to Add JavaScriptJavaScript OutputJavaScript Comments

JavaScript Basics

JavaScript VariablesJavaScript Data TypesJavaScript Operators

JavaScript Statement & Expression

JavaScript StatementsJavaScript ExpressionsJavaScript KeywordsJavaScript IdentifiersJavaScript Case SensitivityJavaScript SemicolonsJavaScript Code Blocks

JavaScript Conditions

JavaScript if StatementJavaScript if else StatementJavaScript else if StatementJavaScript switch Statement

JavaScript Loops

JavaScript for LoopJavaScript while LoopJavaScript do while Loop
  1. Home
  2. /
  3. Tutorials
  4. /
  5. JavaScript Tutorial
  6. /
  7. 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 while loop 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 while loop repeats code while a condition is true.
  • The code block runs before the condition is checked.
  • A do while loop always runs at least once.
  • The condition is written after the while keyword.
  • You must update the loop variable manually.
  • Without a proper update, the loop can run forever.
  • Use do while when 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.

PreviousJavaScript while Loop