while Loops

A JavaScript while loop first checks the value of an expression and starts executing the statements within the loop only if that expression is true. If it is false, execution skips over to the next JavaScript statement (if any).
Upon completing an iteration of the loop, the expression is again tested to see if it is true and the process continues until such a time as the expression evaluates to false, or until execution is otherwise halted.

while (counter < 5)
document.write("Counter: " + counter + "<br />")

If the variable counter were not incremented within the loop, it is quite possible that some browsers could become unresponsive due to a neverending loop, and the page may not even be easy to terminate with Escape or the Stop button. So be careful with your JavaScript loops.


You may also like...