Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • LOOPS IN JAVASCRIPT

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 94
    Comment on it

    LOOPS IN JAVASCRIPT

    A loop allows us to repeat or execute a set of statement multipel times, which are written inside a loop. It execute a block of code many times. Just like all other programming languages javascript also support looping concept.

    javascript contain following types of loops :-

    • for loop
    • do...while loop
    • while loop
    • for...in loop

    FOR LOOP

    The for loop consist of three expressions, with in brackets and separated by semicolons.

    for loop syntax

    for(initialisation;testcondition;increment/decrement)
     {
         statement;
    }
    

    for eg :-

    var temp = 0;
    for (var i = 1; i <= 20; i++)
    {
       temp = temp + i;
    }
    alert("Sum = " + temp);   
    

    do while loop

    The do while loop execute a set of statement atleast once, whether condition is true or not, after executing statement atleast once, then it check the condition if the condition get true, then loop get repeated until condition is true.

    do while loop syntax

    do {
         statement to executed;
         increment/decrement;
    }
    while (condition);
    }
    

    for eg:-

    var temp = 0;
    var number = 1;
    do {
       temp += n;         // -- statement
       n++;              // -- increment/decrement
    } while (n <= 20);   // -- condition
    alert("Sum = " + temp);   
    

    while loop

    Unlike do while loop which execute atleast once wheather condition is true or false, while loop only execute statement if condition get true and continue this process until condtion is true.

    while loop syntax

    while (condition) {
        statement to be executed
    }
    

    for eg:-

     var temp = 0;
        var n = 1;
        while (n <= 50) {  // -- condition
          temp += n;        // -- statement
          n++;             // -- increment/decrement
         }
        alert("Sum = " + temp);  
    

    for..in loop

    for..in loop iterates through the properties of an object .

    syntax of for..in loop

    for (var in object) {
      statement to execute;
    }
    

    for eg:-

    var employee = { name:"suresh", age: 25, degree: "btech" };
    for (var item in employee) {
       alert(employee[item]);  
    }
    

 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: