while Loop through a block of code as long as a variable (i) is less than 5: - Javascript Statement

Javascript examples for Statement:while

Description

while Loop through a block of code as long as a variable (i) is less than 5:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {//from   w  ww .j  av a 2 s .  c  om
    var text = "";
    var i = 0;
    while (i < 5) {
        text += "<br>The number is " + i;
        i++;
    }
    document.getElementById("demo").innerHTML = text;
}
</script>

</body>
</html>

Related Tutorials