Learn Create Your Own Split Screen

Next Page

When you think about it, while loops need three things to make them useful:
  1. A place to start
  2. A condition to know when to keep looping
  3. Something inside the loop that changes, so the condition will eventually no longer be true, and the loop will finish.

Javascript provides a shortcut way of putting all those three things in one place.
var thing= 0; while (thing < 10) // do something interesting thing= thing + 1; }
for (var thing= 0; thing < 10; thing= thing + 1) { // do something interesting }

These two code-fragments do exactly the same thing. The one on the right is simply more concise, and concentrates the three important aspects of loop control in one place.