Javascript looping structure

What are loops?

Loops are control structures that allow sections of code to be executed repeatedly according to the controlling conditions of the loop.

There are three types of loops:

  • Repetition: It will expectute block of code for the times which you specify. if you tell I need this code block to be executed 10 time its simple repetation.In normal work we can call it we are looping throught that code block 10 times.

    • for (i=0; i < 10;i++) {
          // this code block will executed 10 times
      }
  • Iterative loops

    • An iterative loop repeatedly executes a setblock of code as the iterator steps through a series of values availalable in variable. Types of iterative loops include foreach loops. An iterative loop is a repetition but with a variable dependent on the current iteration.

    • This allows the looped code to differ slightly between multiple iterations.

    • For example: The same operation can be perfromed on iteration with differenet value/item.

    • const items = ['item1', 'item2'];
      items.forEach(item => {
          // this code block will executed 2 times
          // and item variable will have `item1` first time
          // and next time it will have `item2`
      });
  • Conditional loops: A conditional loop tests for a condition around the loop, and repeatedly executes a block of code whilst the condition is true. Types of conditional loops include while loops and do-while loops.

    • let i = 0
      while(i < 10) {
          // this loop will execute until i is not 10
          i = i + 1;
      }

If you need more details on the looping structure and want to understand it more please check our detailed article related to each looping structures.

Popular Javascript looping structure articles and code snippets

Related Articles

js interview questions