Java Script


JavaScript Tutorial


Admission Enquiry Form


What is While Loop in JavaScript ?

While loop is also called unknown loop.In JavaScript while loop is used if number of iteration or repetition are not known.The JavaScript while loop iterates the while loop block for the infinite number of times. The syntax of while loop is given below.

Syntax of While loop

initialization;
while(condition)
    {
    //statements.
    increment/decrement;
    } //end of while loop

Example of Nested For loop:

<html>
<head>
<title>While loop</title>
</head>
<body>
<script>
var n=1;
while(n<=5)
{
document.write(n+"<br>");
n++;
}//end of while loop

</script>
</body>
</html>

Output:

1
2
3
4
5

In the above example,initially value of n=1. Then, control will come to while loop and check the condition.It will be true for 5 times ,value of num if less than 5.When it will be incremented to 5 ,then it will be equal to 5.But, after 5 when num value will be incremented to 6 then condition will become false and control will exit the while loop block.