Summary: in this tutorial, you will learn how to use the JavaScript do-while statement to create a loop.
Introduction to the JavaScript do-while statement
The do-while loop statement creates a loop that executes a block of code until a test condition evaluates to false.
The following statement illustrates the syntax of the do-while loop:
do {
statement(s);
} while(expression);Code language: JavaScript (javascript)Unlike the while loop, the do-while loop always executes the body at least once before it evaluates the expression.
Because the expression is evaluated only after the body of the loop has been executed, the do-while loop is called a post-test loop.
Inside the body of the loop, you need to make changes to some variable to ensure that the expression evaluates to false after iterations. Otherwise, you will have an indefinite loop.
Note that from ES6+, the trailing semicolon (;) that follows the while(expression) is optional.

The following flowchart illustrates the do-while loop statement:
JavaScript do-while statement example
See the following example of the do-while loop statement.
let count = 0;
do {
count++;
console.log('count is:' + count);
} while (count < 10);Code language: JavaScript (javascript)In this example, the count variable is set to 0 and is incremented by one in each loop iteration. The loop continues as long as the count is less than 10.
You often use the do-while statement in the situation that the body of the loop needs to execute at least one. This is an important feature of the do-while loop.
The most typical example of using the do-while loop is getting input from the user until the value provided is expected.
Let’s use the do-while loop to develop a simple guessing game. The script generates a random integer between 1 and 12.
You have to guess the number by making guesses until the number you choose matches the number that script chose.
See the following guessing script:
// generate secret number is a random integer between 1 and 12
const MIN = 1;
const MAX = 12;
let secretNumber = Math.floor(Math.random() * (MAX - MIN + 1)) + MIN;
let guesses = 0; // for storing the number of guesses
let hint = ''; // for storing hint
let number = 0;
do {
// get input from user
let input = prompt(`Please enter a number between ${MIN} and ${MAX}` + hint);
// get the integer
number = parseInt(input);
// increase the number of guesses
guesses++;
// check input number with the secret number
// provide hint if needed
if (number > secretNumber) {
hint = ', and less than ' + number;
} else if (number < secretNumber) {
hint = ', and greater than ' + number;
} else if(number == secretNumber) {
alert(`Bravo! you're correct after ${guesses} guess(es).`);
}
} while (number != secretNumber);Code language: JavaScript (javascript)How it works.
- First, generate a random number between the
MIN(included) andMAX(included) function. - Second, get a random integer from the user and check it with the secret number. If the number is different from the secret number, give the user a hint, otherwise, display the congratulation message.
- Third, repeat the second step until the number provided by users matches the generated random number.
In this tutorial, you have learned how to use the do-while loop statement to create a post-test loop that allows the body of the loop to execute at least one and to execute until the test condition evaluates to false.