Edit

JavaScript Loops

Loops are used to execute the same block of code again and again, as long as a certain condition is met. The basic idea behind a loop is to automate the repetitive tasks within a program to save the time and effort.

There are following Loops are used in JavaScript

while — loops through a block of code as long as the condition specified evaluates to true.
do…while — loops through a block of code once; then the condition is evaluated. If the condition is true, the statement is repeated as long as the specified condition is true.
for — loops through a block of code until the counter reaches a specified number.
for…in — loops through the properties of an object.
for…of — loops over iterable objects such as arrays, strings, etc.

The while Loop

The while loop loops through a block of code as long as the specified condition evaluates to true. As soon as the condition fails, the loop is stopped. 

Syntax-:

while(condition) {
    // Code to be executed
}

Example of Code

<script>
    var i = 1;
    while(i <= 5) {    
        document.write("<p>The number is " + i + "</p>");
        i++;
    }
</script>

Output

The do...while Loop

The do-while loop is a variant of the while loop, which evaluates the condition at the end of each loop iteration. With a do-while loop the block of code executed once, and then the condition is evaluated, if the condition is true, the statement is repeated as long as the specified condition evaluated to is true.

Syntax-:

do {
    // Code to be executed
}
while(condition);

Example of Code

<script>
    var i = 1;
    do {
        document.write("<p>The number is " + i + "</p>");
        i++;
    }
    while(i <= 5);
</script>

Output

The for Loop

The for loop repeats a block of code as long as a certain condition is met. It is typically used to execute a block of code for certain number of times. Its syntax is:

Syntax-:

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

The parameters of the for loop statement have following meanings:

initialization — it is used to initialize the counter variables, and evaluated once unconditionally before the first execution of the body of the loop.
condition — it is evaluated at the beginning of each iteration. If it evaluates to true, the loop statements execute. If it evaluates to false, the execution of the loop ends.
increment — it updates the loop counter with a new value each time the loop runs.

Example of Code

<script>
    for(var i=1; i<=5; i++) {
        document.write("<p>The number is " + i + "</p>");
    }
</script>

Output

The for...in Loop

The for-in loop is a special type of a loop that iterates over the properties of an object, or the elements of an array. The generic syntax of the for-in loop is:

Syntax

for(variable in object) {
    // Code to be executed
}

Example of Code

<script>
    // An object with some properties 
    var person = {"name": "Clark", "surname": "Kent", "age": "36"};     
    // Loop through all the properties in the object  
    for(var prop in person) {  
        document.write("<p>" + prop + " = " + person[prop] + "</p>"); 
    }
</script>

Output