While Loop Explained

The general format of a while loop is shown below.
   
    while ( boolean expression )   {
      
      JavaScript Statements
      
    }
    

This looks similar to an if statement, just with the word while instead, but it is radically different.
The reason it's called a loop is that the JavaScript Statements in the body of the loop will keep executing over and over again until the bolean expression becomes false.
The boolean expression is called the loop condition.
The JavaScript Statements in the body of the loop could literally execute millions or billions (or more) of times until the loop condition to become false.

You have to be very careful with loops. If the loop condition never becomes false, you get an infinite loop.
An infinite loop basically never stops running, meaning you might have to force quite the browser or wait until the browser asks you if you want to "terminate the unresponsive script."
The following would be an infinite loop. The loop condition never becomes false.
The variable x keeps getting larger, and the loop condition stays true.
    var x = 1;
    
    while ( x > 0 )   {
      document.write(x);
      x = x + 1;  // adds one to x on each execution of the loop
    }
    
The following loop would not even execute once since the loop condition is initially false.
    var x = 1;
    
    while ( x < 0 )   {
      document.write("This statement will never execute.");
    }
    
When programmers construct them carefully, loops can be very powerful.
The following script output is written by a loop similar to the examples above, but constructed more carefully.
It executes many times and then stops when the loop condition becomes false.