JS while Loop



JavaScript Loop : While Loop in JavaScript (Iterative Statement)

Note :

  • For Single Line of Code – Opening and Closing braces are not needed.
  • while(1) is used for Infinite Loop
  • Initialization , Incrementation and Condition steps are on different Line.
  • While Loop is also Entry Controlled Loop.[i.e conditions are checked if found true then and then only code is executed ]

While Loop Syntax :

initialization;
while(condition)
{
----------
----------
----------
----------
----------
------
incrementation;
}

While Loop

Live Example :

<html>
<body>

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

</body>
</html>

Output :

The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5