The 'while' loop
Learn about the 'while' loop in the 24th Easy JavaScript tutorial, part of EasyProgramming.net
by Nazmus Nasir
HTML
<!-- Easy JavaScript #24 - the while loop-->
<p>
Welcome to the 24th Easy JavaScript tutorial, part of <a href="http://www.easyprogramming.net" target="_blank">EasyProgramming.net</a>. This tutorial covers the <code>while</code> loop!
</p>
<p>
The <code>while</code> loop allows you to run a block of code as long as a specific condition is true.
</p>
<h2>
Syntax of the <code>while</code> loop:
</h2>
<pre>
while (condition is true) {
code to be executed
}
</pre>
<h3>Output:</h3>
<p>
<span id="output"></span>
</p>
<p>
W3Schools: <a href="http://www.w3schools.com/js/js_loop_while.asp" target="_blank">http://www.w3schools.com/js/js_loop_while.asp</a>
</p>
JavaScript
//set the value of 'i' to 0; loop while 'i' is less than 10; increment 'i' by 1 every time it loops
var i = 0;
while(i < 10){
document.getElementById("output").innerHTML += "We are on number " + (i+1) + "<br />";
i++;
}