JavaScript loops are used to repeatedly run a block of code - until a certain condition is met. JavaScript offers several options to repeatedly run a block of code like while, do while, for and for-in.
var hits = 1;
var number = 1;
while (number <= 50)
{
hits += hits;
number++;
}
alert("Hits = " + hits); // => Hits = 1275
The condition is first evaluated. If true, the block of statements following the while statement is executed. This is repeated until the condition becomes false. This is known as a pre-test loop because the condition is evaluated before the block is executed.
The number++ statement is called the updater. Removing it will result in an infinite loop. You must always include a statement in a loop that guarantees the termination of the loop or else you'll run into this problem.