Core JavaScript

While Loops

While loops allow the program to continue looping as long as a boolean value or condition is true. A while loop checks the condition before entering the loop. So, if the condition is false, the loop never executes. This is illustrated in the first loop below. The second loop runs over a set of values for i and stops when the value is 4.

WhileLoops.html

<!DOCTYPE html>
<html>
<head>
    <title>XoaX.net's Javascript</title>
</head>
<body>
    <script type="text/javascript" src="WhileLoops.js"></script>
</body>
</html>

WhileLoops.js

var bKeepLooping = false;
while (bKeepLooping) {
	document.writeln("First Loop Executed<br />");
}


var i = 0;
while (i < 4) {
	document.writeln("Second Loop Executed (i = " + i + ")<br />");
	++i;
}
 

Output

 
 

© 2007–2024 XoaX.net LLC. All rights reserved.