Signup/Sign In
LAST UPDATED: MAY 9, 2020

JavaScript while Loop and do-while Loop

Whenever you want to execute a certain statement over and over again you can use the JavaScript while loop to ease up your work. JavaScript while loop lets us iterate the code block as long as the specified condition is true. Just like for Loop, the while and do...while loop are also used to execute code statements multiple times based on a condition.

The only difference between the while loop and do...while loop is that the do...while loop will execute at least once because in do...while loop the condition is checked after execution of code block.

JavaScript while Loop: Syntax and Use

Below is the syntax for the while loop:

while(condition)
{
    // code statements
}

Let's take an example to see the while loop in action.

JavaScript while Loop: Example

In this example, we are using while loop, and the loop executes only if the specified condition is true.

JavaScript do…while Loop: Syntax and Use

Just like the while loop, the do...while loop lets you iterate the code block as long as the specified condition is true. In the do-while loop, the condition is checked after executing the loop. So, even if the condition is true or false, the code block will be executed for at least one time.

Below we have the syntax of the do...while loop,

do
{
    // code statements
}
while(condition)

JavaScript do...while Loop: Example

Let's take an example and see the do...while loop in action.

In this tutorial, we explained the while and do...while loops used in the JavaScript.



About the author:
I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight