While and loop statements

Note Whenever programming a loop statement, ensure that the exit condition will be met; otherwise, an infinite loop will occur.

For detailed information about the for and while loop statements, refer to the Statements – Loop Statements section in:

http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide

The following example shows how to check how many times a given number can be divided by 2, first using a for loop, and then using a while loop:

function DivisableByTwoFOR(ParmNumber)
{
	for (var i=0; ParmNumber > 1; i++)
	{
		ParmNumber = ParmNumber / 2;	
	}
return i;
}
function DivisableByTwoWHILE(ParmNumber)
{
	var i = 0;
	while( ParmNumber > 1 )
	{
		ParmNumber = ParmNumber / 2;
		i++;
	}
return i;
}

Note A Do..While loop is available for those instances when you want to execute a statement (or block of statements) at least once. In a Do..While loop, the exit condition is checked after the statements are executed.

Caution The for…in loop over an array may return unexpected results. For details, see Avoiding the use of the for…in loop over an array.