JavaScript While Loop

As it is described in last chapter that JavaScript loops are used for iteration to the piece of code using different loops like For loop, while loop or do-while loop etc. It create easiness in the programm. It is mostly used in array.
There are four types of loops in JavaScript.
1- for loop
2- while loop
3- do-while loop
4- for-in loop

Example of While Loop

In this example syntax of while loops is described. The JavaScript while loop 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
}

<script type = "text/javascript">
var i=11;  
while (i<=15)  
{  
document.write(i + "<br/>");  
i++;  
}     
</script>                  
        

Output of above code


Example of Do/While Loop

In this example syntax of do/while loops is described. The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.

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

<script type = "text/javascript">
var text = ""
var i = 0;
  do {
      text += "The number is " + i "<br/>";
      i++;
    }
  while (i < 10); 
  document.write(text);    
</script>                  
        

Output of above code








The Best

Comment here

If you have any query, if you want to know something about any of technical course related to computer science field, if you have any suggestion about relevant to uploaded content or if you anything wrong here (any mistake in content) than please contact us. Keep in mind, comment should be according to community guidelines.