Loops in JavaScript

Loops: These are the iteration statements used when we want to run single or multiple statements a multiple number of times.

They are of the following 3 types in JavaScript:

  •  while loop: It processes a statement a fixed number of times and continiues to iterate until the condition specified by the user becomes false.

Syntax

  1. initialization;  
  2. while (condition)  
  3. {  
  4.     statements;  
  5.     increment / decrement;  

Example

  1. <script type = "text/javascript" >  
  2. var target = Number(prompt("Please Enter a number"""));  
  3. var start = 0;  
  4. while (start <= target)  
  5. {  
  6.     document.write(start + "<br/>");  
  7.     start += 2;  
  8. }  
  9. </script> 

The preceding code displays all the even numbers until the targeted value that the user entered into the prompt box. The Number operator will type cast the value to a number format.

The following are some points to remember:

  • If we do not use increment or decrement in a value, then the loop will continue running infinitely.
  • If we want to limit the execution of the loop, the break statement is used. For example:

 

  1. <script type = "text/javascript" >  
  2. var target = Number(prompt("Please Enter a number"""));  
  3. var start = 0;  
  4. while (start <= target)  
  5. {  
  6.     document.write(start + "<br/>");  
  7.     start += 2;  
  8.     if (start > 100)  
  9.     {  
  10.         break;  
  11.     }  
  12. }   
  13. </script> 

In the preceding example, the loop will stop when the value of start goes above 100.

  • If we want to skip the iteration of the loop at some point, then we use the Continue statement. For example:

 

  1. <script type = "text/javascript" >  
  2. var start = 0;  
  3. while (start < 10)  
  4. {  
  5.     start += 1;  
  6.     if (start % 2 == 0)  
  7.     {  
  8.         continue;  
  9.     }  
  10.     document.write(start + "<br/>");  
  11. }   
  12. </script> 

In the preceding code, the loop will skip all the even values and displays only the odd values until 10.

  • do while loop: This loop works the same as a while loop, the only difference is that it checks the condition at the last (bottom). This means that it will execute at least one time and then checks the condition that it is true or false. If the condition is true, it will continue executing and once the condition becomes false, it stops and stops the iteration.

Syntax

  1. initialization;  
  2. do  
  3. {  
  4.    statements;  
  5.    increment/decrement;  
  6. }
  7. while (condition); 

Example

  1. <script type = "text/javascript" > do   
  2. {  
  3.     var target = Number(prompt("Please Enter a number"""));  
  4.     var start = 0;  
  5.     while (start <= target)  
  6.     {  
  7.         document.write(start + "<br/>");  
  8.         start += 2;  
  9.     }  
  10.     do  
  11.     {  
  12.       userChoice = prompt("Do you want to continue? - Yes / No """).toUpperCase();  
  13.         if (userChoice != "YES" && userChoice != "NO")  
  14.         {  
  15.             alert('Please Enter a valid choice.');  
  16.         }  
  17.     }  
  18.     while (userChoice != "YES" && userChoice != "NO");  
  19. }  
  20. while (userChoice == "YES"); < /script> 

The preceding code displays the even numbers as explained above in the while loop. Just we have added is that, now the program asks the user to select whether he/she wants to continue to use the program or exit through it. This is done using a do while loop.

The toUpperCase() method is used to change the case of the text entered by the user. If the user enters some lowercase letters and some upercase letters, then our if condition will not work as expected.

  • for loop: It is the most used and the easiest iteration loop. It works the same as the while and do while loop, the only difference is that it takes all the parameters in the same line.

Syntax

  1. for (initialization; condition; increment/decrement)  
  2. {  
  3.    statements;  

Example

  1. <script type = "text/javascript" >  
  2. var target = Number(prompt("Enter a Number:"""));  
  3. for (var start = 0; start <= target; start += 2)  
  4. {  
  5.     document.write(start + "<br/>");  
  6. }
  7. </script> 

The preceding code, takes all the values in the same line. So, it is easy to maintain and use.

For any queries, please leave a comment.

Up Next
    Ebook Download
    View all
    Learn
    View all