JavaScript while Loop


Loops execute a block of code a specified number of times, or while a specified condition is true.

While Loop
 while ( condition ) {
   code to be executed
 }

The while loop loops through a block of code while a specified condition is true. There are two special statements that can be used inside loops: break and continue. The break statement will break the loop and continue executing the code that follows after the loop (if any).



   

Do ... While Loop
 do {
   code to be executed
 }
 while ( condition );

The do ... while loop is a variant of the while loop. This loop will execute the block of code once, and then it will repeat the loop as long as the specified condition is true. The continue statement will break the current loop and continue with the next value.



   


Demonstration
The following demonstration shows how the script of HTML and JavaScript is displayed on the Web.

     





      An Englishman teaching an American about food is like the blind leading the one-eyed.    
      — A. J. Liebling