Javascript while loop TUTORIAL

While Loop

The JavaScript while loop consists of a condition and the statement block.

while (condition)

The condition is evaluated. If the condition is true the statements are evaluated. If the statement is false we exit from the while loop.

Let us take a simple example below that prints the number from 0 to 10.

<script type="text/javascript">
Example While loop
var i=0;
while (i<=10)
document.write("The number is " + i);
document.write("<br />");

The example defines the while loop that starts with i=0. The loop will continue to run as long as i is less than, or equal to 10. The value of i will increase by 1 each time the loop runs.

You can try this example online at the link below, which will open in a new window.

Javascript while loop - Example