JavaScript Loops

Looping is a fundamental programming idea that is commonly used in writing programs. A loop is a sequence of instruction s that is continually repeated until a certain condition is reached. It offers a quick and easy way to do something repeatedly.

There are four loops in JavaScript programming:

  1. for loop
  2. for-in loop
  3. while loop
  4. do-while loop

JavaScript for..loop

Javascript for loop is the most widely used type of loop and it repeats until a specified condition evaluates to false. Syntax
for (initialization; condition; increment) { code to be executed }
example
for (i=1; i<=5; i++){ alert(i); }
  1. i=1 : initialization
  2. i < =5 : condition
  3. i++ : increment

When you run the above program it will display number 1 to 5 in message box.

JavaScript for-in loop

JavaScript for-in loop is convenient to use with arrays or object, and it enables the user to loop through each element in the array with having to know the number of element the array actually contains. Rather than working with index number of each element, it automatically moves to the next index with each iteration. Syntax
for(index in arrayName){ code to be executed }
example
var days = new Array("Sunday","Monday","Tuesday"); var day; for (day in days) { alert(days[day]); }

JavaScript while loop

JavaScript while loops can execute a block of code as long as a specified condition is true. It enables you to test a condition and keep on looping when it meets the specified condition . The loop will terminate when its condition evaluates to false. Syntax
while (condition) { code to be executed }
example
var num=1; while (num <= 5) { alert(num); num ++; }

Javascript do..while loop

The do-while loop is executed at least once whereas the while loop may not execute at all. That means, it will execute the code block once, before checking if the condition is true , then it will repeat the loop as long as the condition is true. Syntax
do{ code to be executed }while (condition);
example
var i=1; do{ alert("value of i is " + i); i++; }while (i>5);
When you run the above example, it will execute one time even though the condition is not met (i>5).

Difference between while loop and do while loop

A do-while loop guarantees the execution of the loop at least once because it checks the loop condition AFTER the loop iteration. Whereas a while loop will check the condition first before executing the content.

Javascript Infinite Loop

A loop that repeats indefinitely and never terminates is called an Infinite loop or endless loop.

In JavaScript, by omitting all parts of the head, the loop can also become infinite:

for (;;) {}

You can use a while loop as infinite loop:

while (true) { //your code }

Javascript Break statement

Executing the break statement exits from the current loop or statement , and begins script execution with the statement immediately following. example
var i=1 while (i <= 5) { if (i==3) break; document.write( i + " - inside loop <br>" ); i ++ } document.write("Exit from loop...." );
output
1 - inside loop 2 - inside loop Exit from loop....

Javascript Continue statement

Javascript Continue statement stops the current iteration of a loop, and starts a new iteration. example
var i=0 while (i <= 4) { i ++; if (i==2) continue; document.write( i + " number of time <br>" ); }
output
1 number of time 3 number of time 4 number of time 5 number of time