Javascript While Loop

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


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

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

Javascript While Loop Example

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

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.