While Loop and Do
/*
The while statement performs a simple loop. If the expression is falsy, then the loop will break.
While the expression is truthy, the block will be executed.
*/
<!DOCTYPE html>
<html>
<body>
<p>This loops until i is less than 20</p>
<button onclick="myWhileFunction()">Try it</button>
<p id="demo"></p>
<script>
function myWhileFunction() {
var text = "";
var i = 0;
while (i < 20) {
text += "<br> The number is " + i;
i++;
}
document.getElementById("demo").innerHTML = text;
}
</script>
</body>
</html>
// Variant, do loop, code block will always be executed at least once
<script>
function myFunction() {
var text = ""
var i = 0;
do {
text += "<br>The number is " + i;
i++;
}
while (i < 10)
document.getElementById("demo").innerHTML = text;
}
</script>