While Loop Demo: Print 0 to 5
This keeps the original demo result while using DOM output instead of document.write().
Complete Working Source Top ↑
<!doctype html>
<class="text-primary">html
lang=
"en">
<
class="text-primary">head>
<
class="text-primary">meta
charset=
"utf-8">
<
class="text-primary">meta
name=
"viewport" content=
"width=device-width, initial-scale=1">
<
class="text-primary">title>While Loop Demo: 0 to 5</
class="text-primary">title>
<
class="text-primary">style>#output { white-space: pre-line; }</
class="text-primary">style>
</
class="text-primary">head>
<
class="text-primary">body>
<
class="text-primary">div
id=
"output" aria-live=
"polite"></
class="text-primary">div>
<
class="text-primary">script>
const output = document.getElementById(
"output");
let text =
"";
let i = 0;
while (i <= 5) {
text += `${i}\n`;
i += 1;
}
output.textContent = text;
</
class="text-primary">script>
</
class="text-primary">body>
</
class="text-primary">html>
Return to tutorial on While loop
Author & Instructor at
plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery,
and web development at plus2net. The tutorials focus on clear explanations, working
examples, and code that readers can test and adapt while learning.
← Subscribe to our YouTube Channel here
This article is written by plus2net.com team.
plus2net.com