Loading Please Wait...

Logo Lynxsia IT Solutions

JavaScript Loops

JS Loops

JavaScript loops are used to execute a single block of code multiple times with different values. For example, working with arrays, strings, objects, etc.

Types Of Loops

JavaScript have different types of loops listed below:

  • for
  • while
  • do-while
  • for-in
  • for-of
The for Loop

The for statement creates a loop with 3 optional expressions.

					 
        
          for (expression 1; expression 2; expression 3) {
            // code block to be executed
          }
        
      

Expression 1 is executed (one time) before the execution of the code block. Normally used to initialize the first value.

Expression 2 defines the condition for executing the code block.

Expression 3 is executed (every time) after the code block has been executed. Normally change the value to next step.

					 
        
          for (let i = 0; i < 5; i++) {
            text += "The number is " + i + "<br>";
          }
        
      
Expression 1

You can initiate many values in expression 1 separated by comma and can omit expression 1 like when your values are set before the loop starts.

					 
        
          for (let i = 0 , len = 5, text = ""; i < len; i++) {
            text += "The number is " + i + "<br>";
          }

          // Omitting the expression 1
          let j = 0;
          for (; j < 5; j++) {
            text2 += "The number is " + j + "<br>";
          }
        
      
Expression 2

If expression 2 returns true, the loop will start over again. If it returns false, the loop will end.

If you omit expression 2, you must provide a break inside the loop. Otherwise the loop will never end. This will crash your browser.

					 
        
          // Omitting the expression 1 and 2
          let i = 0;
          for (; ; i++) {
            text += "The number is " + i + "<br>";
            if(i === 5){ break; }
          }
        
      
Expression 3

Expression 3 can do anything like negative increment (i--), positive increment (i = i + 15), or anything else. Expression 3 can also be omitted (like when you increment your values inside the loop).

					 
        
          // Omitting the expression 1, 2 and 3
          let i = 0;
          for (; ;) {
            text += "The number is " + i + "<br>";
            if(i === 5){ break; }
            i++;
          }
        
      
The While Loop

The while loop loops through a block of code as long as a specified condition is true.

Syntax
					 
        
          while (condition) {
            // code block to be executed
          }
        
      
					 
        
          while (i < 10) {
            text += "The number is " + i;
            i++;
          }
        
      
The Do While Loop

The do while loop is a variant of the while loop. This loop 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. This guaranteed that the loop must execute the code block at least once.

Syntax
					 
        
          do {
            // code block to be executed
          } while (condition);
        
      
					 
        
          do {
            text += "The number is " + i;
            i++;
          } while (i < 10);
        
      
The For In Loop

The for in statement loops through the properties (key index) of an Object/array.

The for in loop iterates over an object/array. In each iteration it returns a key that can be used to access the object/array.

The for in does not guaranteed to iterate object/array in index order. It may select key in random order. So it is better to use for, for of, or Array.forEach() loops

Syntax
					 
        
          for (key in object) {
            // code block to be executed
          }
        
      
					 
        
          const numbers = [45, 4, 9, 16, 25];

          let txt = "";
          for (let x in numbers) {
            txt += numbers[x];
          }
        
      
The For Of Loop

The for of statement loops through the values of an iterable object/array.

The for of loop iterates over an object/array. In each iteration it returns the value.

Syntax
					 
        
          for (variable of iterable) {
            // code block to be executed
          }
        
      
					 
        
          const cars = ["BMW", "Volvo", "Mini"];

          let text = "";
          for (let x of cars) {
            text += x;
          }
        
      

How you feel about this blog:

Share this blog on:

Report Us

If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com

Contact Us
Ads
Logo
Lynxsia IT Solutions

We are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..

Kotwali Road, Chhiptehri, Banda, 210001, UP, India

Copyright © 2022, Lynxsia IT Solutions, All rights reserved