JavaScript Loops

Here is list of JavaScript loops.

  1. for loop
  2. do-while loop
  3. while loop
  4. for-in loop
  5. break statement
  6. continue statement

for loop

for loop is most frequently used loop in JavaScript.

Syntax

for (initialExpression ; condition ; incrementExpression) { // code block to be executed }

initialExpression is executed before the loop starts.

condition defines the condition for running the loop.If the value of condition is true, then only the loop statements execute.

incrementExpression is executed each time after the loop has been executed.

Example

<script language="javascript" type="text/javascript" >
var total=0;
for (i = 0; i < 10; i++) {
  total = total + i;
}
document.write(total);
</script>

do-while loop

do-while loop is executed at least once whereas the while loop may not execute at all.

do block is executed first and then the condition is checked.The while block is executed again and repeats until the condition becomes false.

Syntax

do { // code block to be executed } while (condition);

Example

<script language="javascript" type="text/javascript" >
var total = 0;
var number = 1;
do {
   total += number;         
   number++;              //-- loop incrementExpression
} while (number <= 10);   // -- condition
document.write("Total = " + total);
</script>

while loop

while loops are used to repeatedly run a block of code until the condition becomes false.

Syntax

while (condition) { // code block to be executed }

Example

<script language="javascript" type="text/javascript" >
var total = 0;
var number = 1;
while (number <= 10) {  // -- while loop condition
  total += number;      // -- loop body
  number++;             // -- loop incrementExpression
}
document.write("Total = " + total);  
</script>

The number++ statement is called the incrementExpression. Removing it will result in an infinite loop. You must always include a statement in a loop that guarantees the termination of the loop other wise it will create crash your browser.

for-in loop

for-in loop iterates through the properties of an object.Here is an example of for-in.

Example

<script language="javascript" type="text/javascript" >
var student = { name:"Max", age: 28, degree: "Mca" };
for (var item in student) {
   alert(student[item]);     // => "Max", then 28, then "Mca"
}
</script>

break statement

break statement is used to "jumps out" from loop.

Example

<script language="javascript" type="text/javascript" >
var total = 0;
for (var i = 1; i <= 100; i++) {
   total += i;
   if (i === 15) {
       break;    // immediately transfers control outside the for block
   }
}
document.write("Total = " + total);       // => Sum = 120
</script>

continue statement

continue statement is used to "jumps over" one iteration in the loop.

Example

<script language="javascript" type="text/javascript" >
for (var i = 1; i <= 8; i++)
{
   if ((i % 2) != 0) {
      continue;
   }
   document.write(i+"<br>") ;          // => 2, 4, 6, 8
}
</script>