JavaScript Loop While
JavaScript Tutorial » JavaScript Loop While
JavaScript Loop While statement repeatedly executes a sequence of code as long as a condition is true.
Syntax:
Example:
while (condition) {
// code block to be executed
}
Consider the following example, code in the loop will run, over and over again, as long as a variable (i) is less than 6:.
Example:
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript While Loop</h1>
<p id="my_demo"></p>
<script>
var my_text = "";
var i = 0;
while (i < 6) {
my_text += "<br>My number is " + i;
i++;
}
document.getElementById("my_demo").innerHTML = my_text;
</script>
</html>
Do/While Loop - example
do/while loop is a variant of the while loop.
Do/While loop has the following syntax:
do {
// code block to be executed
}
while (condition);
// code block to be executed
}
while (condition);
example below uses a do/while loop
Example:
Output:
JS Do/While Loop
My number is 0
My number is 1
My number is 2
My number is 3
My number is 4
My number is 5
<!DOCTYPE html>
<html>
<body><h1>JS Do/While Loop</h1>
<p id="my_demo"></p>
<script>
var my_text = ""
var i = 0;do {
my_text += "<br>My number is " + i;
i++;
}
while (i < 6);document.getElementById("my_demo").innerHTML = my_text;
</body>
</script>
</html>
JS Do/While Loop
My number is 0
My number is 1
My number is 2
My number is 3
My number is 4
My number is 5
javascript loop while true, false, wait, break, example, continue, array, syntax, async
javascript loop while true, false, wait, break, example, continue, array, syntax, async
JavaScript Loop While - javascript tutorial
Online Editor
This tool makes it easy to create, adjust, and experiment with custom colors for the web.
HTML Templates

Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.
CSS HTML Layout

Find here examples of creative and unique website layouts.
Free CSS HTML Menu

Find here examples of creative and unique website CSS HTML menu.