A loop statement is one that will run repeatedly until a certain condition is met.

JavaScript has the following types of loop statements:

  • for(){...}
  • do{...}while();
  • while(){...}
  • for each(in){...}

Each type of loop statement has specific ways of testing for the condition. One may be a better fit than others for certain situations.


The for(){...} loop statement

The for(){...} loop statement puts the condition at the top of the loop. If the condition is not met, the loop will run. If the condition has already been met, the loop will not run, not even once.

The for(){...} loop has the conditon to be tested and the incrementing included in the beginning of the loop. This is not true for other loop types.

Three parameters go into the parentheses of the for(){...} loop separated by a semi-colon ;.

for ([initial expression]; [condition to be met]; [increment expression])

The [initial expression] is this first expression to get things going. It is often something like var ctr = 0, which declares a variable named ctr and sets its value to 0. The ctr variable will be what is incremented each time the loop is run with the [increment expression].

The [condition to be met] is what will stop the loop from running once this point is reached. It will often be something like ctr == 20

The [increment expression] will be something like ctr++. This means that the ctr variable value will increment by 1 each time the loop is run. This is necessary so the loop won't run forever/

Here is an example of a for(){...} loop statement:

for (ctr = 0; ctr < 25; ctr++){
   /// steps go here


The do{...}while(); loop statement

With the do{...}while(); statement will alway run at least once. The condition to be tested is contained in the parentheses after the word while(). If the condition is true, the loop will run again. It will run until this condition tests to be false.

You must include the incrementing or change of the parameter being tested in with the statements, as if is not included inside the parentheses after while().

Here is an example of a do{...}while(); loop statement:

do {
    // steps go here
} while (ctr < 10);


The while(){...} loop statement

The while(){...} loop statement will test the condition, if true will run the series of steps. It will continue to do so until the condition tests false.

You must include some sort of incrementing or change of the parameter being tested or the loop will run forever.

Here is an example of a while(){...} loop statement:

while (x < 40){
    // steps go here
    x = x + 1;


The for each(in){...} loop statement

The for each(in){...} loop statement will iterate over the values of the properties of the object, not its name. You place the word in inside of the parentheses. This is often used with object literals and their key:value pairs. It iterates through each set of key:value pairs.

var total = 0;
var testVal = {propA: 60, propB: 80, propC: 100}; // this is an object literal
for each (var item in testVal) {
    total += item;


Getting out of a loop statement, the break; statement

There is a way of exiting a loop statement other than testing if the condition is true. This is by using the break; statement. 

The break; statement will terminate a loop statement or a switch statement.


Halt an inner nested loop, but continue with outer loop, the continue; statement

If you have a case of nested loops, you want to test for a certain condition in the inner loop and stop it, then go back to running the outer loop, you use the continue; statement.