Javascript Loop While

This statement iterates the elements for the infinite number of times. It should be used if number of iteration is not known. The syntax of while loop is given below:-

Syntax:-
while (condition)  
{  
    code to be executed  
}  

Let’s see the simple example of while loop in javascript.

Input:-
 <script>  
	   var i=11;  
    while (i<=15)  {  
        document.write(i + "<br/>");  
        i++;  
    }  
</script>

Output:-


Do while() loop

This loop iterates the elements for the infinite number of times like while loop. But, code is executed at least once whether condition is true or false. The syntax of do while loop is given below.

Syntax:-
do{  
    //code to be executed  
}while (condition);    

Let’s see the simple example of do while loop in javascript.

Input:-
 <script>  
	   var i=21;  
    do{  
       document.write(i + "<br/>");  
       i++;  
    }while (i<=25); 
</script>

Output:-