Javascript While Loop

The Javascript while loop statement will execute a block of code when a given condition is true.

Syntax

<script type="text/javascript">
while (variable <= endvalue) {
    //code block to be executed
}
</script>

Note: The <= could be anything that would fit the purpose ex. >, == or whatever.

Javascript While Loop Example

<!DOCTYPE html>
<html>
<head>  <!-- www.techstrikers.com -->
<title>My first Javascript code</title>   
</head>  
<body bgcolor="#bnde45">
<fieldset>
<legend>Javascript While Loop</legend>
<script type="text/javascript">
var i=0 ;
while (i<=10)
{
document.write("<b>" + "The number is " + i + "</b>")
document.write("</br>")
i++
}
</script>
</fieldset>
</body>
</html>

The while loop sets i equal to 0. As long as i is less than or equal to 10, the loop will continue to run. i will increase by 1 each time the loop runs.