The while statement is a loop executing kind of a statement used to pre-test flow conditions.

The while loop statement is called a pretesting because the expression is tested before the loop starts and if it fails, the loop will never initiate, as shown in the example:

while (expression) {



Or in the real numbers example:

var counter = 50;

var i =4;

for (i<counter) {


   i += 7;


In the example above, the variables (counter, limit) must be initialized before the loop starts testing for the expression. If the expression evaluates to true (above: i < counter), the loop starts by showing the current value, and increasing it for "7", after which the next loop starts. The iteration will last till the counted number (i) reaches the limit (counter).


The example with while statement in JavaScript:


›› go to examples ››