JavaScript Example: Do While Loop
x
<title>JavaScript Example: Do While Loop</title>
<div id="msg"></div>
<script>
// Set variables
var myBankBalance = 0;
var output = "";
output += "<p>My bank balance is:</p>";
output += "<ul>";
// The loop
do {
output += "<li>$" + myBankBalance + "</li>";
myBankBalance ++;
}
while (myBankBalance <= 10);
output += "</ul>";
// Output results to the above HTML element
document.getElementById("msg").innerHTML = output;
</script>
In this example, the left side provides the code used to render the output in the right side.
Feel free to copy and paste the code into your own project and modify as you wish.
About JavaScript
JavaScript is a scripting language that enables us to add functionality to web pages. We can combine JavaScript with our HTML code to achieve this outcome. We typically enclose our JavaScript inside <script>
tags, but we can also use event handlers (such as onclick
, onchange
, onmouseover
etc) within the HTML tags themselves to trigger the JavaScript.