Javascript Loops

Javascript Loops

Loops are used to do some task or execute some statements or block of code number of times as specified


Javascript has 4 different types of loops

  • For Loop
  • While Loop
  • DoWhile Loop
  • For..in Loop
  • For Loop

    For loop is used where when we know the number of time we want to execute the code.


    Syntax


    for (initialization; condition; increment)
    {
      code to be executed;
    }
    


    Code Explanation

  • Initializing value is the starting value of the counter
  • Condition is the conditional used to stop the loop execution
  • Increment is the increment of the initializing value

  • Example of For Loop

    var a = 0;
    var b = 0;
    
    for( var i=0; i<5; i++ )
    {
        a ++;
        b ++;
    }
    document.write ("At the end of the loop a=5 and b=5" );
    



    While Loop

    While loop is used where we want to execute the code as long as the condition is true.


    Syntax


    while (condition)
    {
        code to be executed;
    }
    


    Code Explanation

  • Condition is the conditional used to stop the loop execution

  • Example of While Loop

    var a = 0;
    var b = 0;
    
    while( var i < 5)
    {
        a ++;
        b ++;
    }
    document.write ("At the end of the loop a=5 and b=5" );
    



    Do While Loop

    Do While loop is used where we want to execute the code at least one time and then it repeats the loop as long as the condition remain true.


    Syntax


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


    Code Explanation

  • Condition is the conditional used to stop the loop execution

  • Example of Do While Loop

    var a = 0;
    var b = 0;
    
    do
    {
        a ++;
        b ++;
    }
    while( var i < 5)
    
    document.write ("At the end of the loop a=5 and b=5" );
    



    For..in Loop

    For each loop is used to loop through an objects properties.


    Syntax


    for(variableName in object)
    {
        code to be executed;
    }
    


    Code Explanation

  • In every increment one property of an object is assigned to variableName and this loop continues till all the properties of the object are finished.

  • Example of For..in Loop

    var name = {firstname:"John", lastname:"Doe"}; 
    
    var container = " ";
    var i;
    for (i in person) {
        container += name[i];
    


    Continue Statements

    Continue is used to stop the current operation of a loop when the condition satisfy but it does not terminate the loop


    for( var i=0; i<10; i++ )
    {
      if( i == 5 )continue;
      echo "Value is ".i;
    }
    

    This will display 1 to 10 execpt 5 because the condition statify on var i=5



    Break Statements

    Break is used to stop the loop when the condition satisfy.


    for( var i=0; i<10; i++ )
    {
      echo "Value is ".i;
      if( i == 5 )break;
    }
    

    This will display 1 to 5 because the condition statify on var i=5 and the loop terminates

    PreviousNext

    Join Us With Our 21000+ Subscribers And Get Our Latest Tutorials Update Via Email