JavaScript Do While

The JavaScript Do While will test the given condition at the end of the loop. So, Do While loop executes the statements inside the code block at least once, even if the given condition Fails.

The While loop we discussed in our previous Js article tests the condition before entering into the code block. If the condition is True, then only statements within this loop will be executed. Otherwise, statements will not execute at least once.

There are some situations where it is necessary to perform some operations (execute some statements) first and then check for the condition. In these cases, we can go for Do While loop.

JavaScript Do While Loop Syntax

The syntax of the Do While Loop is

  statement 1;
  statement 2;
  statement n;
While (condition);

First, it will execute the statements inside this do while loop. Then after reaching the end, it will check the condition inside the while. If the condition is True, then it will repeat the process. If the condition fails, then the iteration is terminated.

JavaScript Do While Loop Flow Chart

The flow chart of it is shown below.

Flow Chart

The flow chart sequence of a do while loop in JavaScript Programming Language is:

  1. First, we initialize our variables. Next, it will enter into the loop.
  2. It will execute the group of statements inside the loop.
  3. Next, we have to use JavaScript Increment and Decrement operators inside the loop to Increment and Decrement values.
  4. Now it will check the condition. If the condition is True, then the statement inside it will execute again. It will continue the process as long as the condition is True.
  5. If the condition is False, it will exit from the loop

JavaScript Do While Loop Example

This program helps us to understand the Do While Loop.

  1. First, we declared the variable number and initialized it to 6. And initialized the total variable to 0.
  2. Next, within this, the number has been added to the total.
  3. In the next line, we used the ++ operator to increment the number value.
  4. After this line, the number has been tested against the while condition. If the condition results true, then it will repeat the process. Otherwise, it will exit from the loop.
  5. In the next line, we used one more statement to show that it comes from outside the JavaScript do while loop.
<!DOCTYPE html>
    <title> Example </title>

    <h1> JavaScriptDoWhile </h1>
    var number = 6, total=0;
        total = total + number;
        document.write("<br\>Number = " + number);
        document.write("<br\>Total Value is = " + total);
    }while (number <= 10);
    document.write("<br\>Total Value from outside the Loop is = ", total);  
JavaScript Do While Loop 1

Let us enter a value greater than 10 to see what will happen. If you observe from the above screenshot, we entered the value 11, and it still displays the total as 11. Because after executing that code, it checked the while condition, and it failed, so it exited from the loop.


Number = 12
Total Value is = 12
Total Value from outside the Loop is = 12