1let i = 2;
2while (i < 10) {
3 text += "The number is " + i;
4 i++;
5}
6
7const cars = ["audi", "bmw", "mercedes", "porsche"];
8let i = 0;
9let text = "";
10
11while (cars[i]) {
12 text += cars[i];
13 i++;
14}
Do While Loop Usage
This loop will execute the code block once, before checking if the condition is true or false, then it will repeat the loop as long as the condition is true.
1let i = 0;
2let text = "";
3
4do {
5 text += "The number is " + i;
6 i++;
7}
8while (i < 10);
While Loop Usage in HTML
1<!DOCTYPE html>
2<html>
3<body>
4
5<p id="demo"></p>
6
7<script>
8const cars = ["audi", "bmw", "mercedes", "porsche"];
9let i = 0;
10let text = "";
11
12while (cars[i]) {
13 text += cars[i] + "<br/>";
14 i++;
15}
16
17document.getElementById("demo").innerHTML = text;
18</script>
19
20</body>
21</html>