Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Javascript Looping Array Elements

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 119
    Comment on it

    Basically Javascript previous version ES5(script 5) support three loops and new version ES6(script 6) version support two more loops i.e for-of loop (ES6 ) and an iterator (ES6) .

    1) for each loop
    for each() method iterates the specific variables over all the values of object's properties .For each different statement , a specified statement is executed .
    Syntax ->

    for each ( variable in object ){
    //statements;
    }
    

    variable -> variable is to iterate the values ,and is declared withe the var keyword , and this variable is local to the function , not to the loop.
    object ->object for which the properties are iterated .
    statement ->A statements is used to execute for each property . To execute multiple statements within the loop use a block statement ( {.... } ) like this . Example ->
    var sum = 0;

    var values = { a :5 , b : 10 , c : 15 } ;
    for each ( var item in values ){
    sum = sum + item ;
    }
    console.log(sum); // output is -> 30 i.e 5 + 10 +15 .<br><br>
    

    2) for loop
    simple for loop is used to create a loop that consists three optional expressions , which comes in paranthesis and separted by semicolons and followed by a statement or multiple statements .
    Syntax ->

    for( initialize ; , condition ; , final-expression ;){
    statement
    }
    

    initialize -> In this there is a variable declaration or used to initialize a counter variable. This expression may be optional and declare with the var keyword. These variables are not local to the loop, i.e they are in the same scope .
    condition ->
    This check the given condition and evaluated before each loop iteration .If this expression is true than the statement will be executed . And this is also a optional .If you omit this expression then the condition always evaluates as a true .
    final expression -> This expression is evaluated at the end of each loop iteration. Generally it is used to update or increment the counter variable .
    Example ->

    for (var i = 0; i < 10 ; i++){
    console.log(i); //output ->0 1 2 3 4 5 6 7 8 9
    }
    

    Optional for expression -> 1) when initialize expression is not required

    var i =0;
    for( ; i < 10 ; i++ ){
    console.log(i); //output ->0 1 2 3 4 5 6 7 8 9
    }
    

    2) when condition expression is not required

     for(var i =0; ; i++ ){
        console.log(i); 
    if(i >3) 
    break;
        }
    

    3) when all three expression is omit

    var i = 0;     
         for(; ;){
            if(i > 3)
            break;
      console.log(i);
      i++;
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: