Course Content
Introduction to JavaScript
4. Conditional Statements
Introduction to JavaScript
do while
Let's consider the do-while loop. This loop is similar to the while loop but differs.
while | do-while |
Executes code block after the conditional checking. | Executes code block before the conditional checking. |
Syntax
The do-while loop initiates with the do
keyword, encompasses the code block ({}
), and concludes with the while
keyword followed by the condition (()
) without the code block:
Here's an example:
At the start, the variable a
was set to 0
, but the condition a >= 1
was assessed after the a++
operation.

The code block within the do
is always executed at least once:
Note
Including the end-of-command (
;
) after thedo
code block results in aSyntaxError
.
The do-while loop is handy when you need to prompt the user for input at least once and continue until the user enters valid data or meets a specific condition.
Everything was clear?