while Statement (JavaScript)

Executes a statement or series of statements until a specified condition is false.

Syntax

while (expression) {  
    statements  
}   

Parameters

expression
Required. A Boolean expression that is checked before each iteration of the loop. If expression is true, the loop is executed. If expression is false, the loop is terminated.

statements
Optional. One or more statements to be executed if expression is true.

Remarks

The while statement checks expression before a loop is first executed. If expression is false at this time, the loop is never executed.

Example

The following example illustrates the use of the while statement.

var i = 0;  
var j = 10;  
while (i < 100) {  
    if (i == j)  
        break;  
    i++;  
}  
document.write(i);  

// Output: 10  

Requirements

Supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards, Internet Explorer 9 standards, Internet Explorer 10 standards, Internet Explorer 11 standards. Also supported in Store apps (Windows 8 and Windows Phone 8.1). See Version Information.

See Also

break Statement
continue Statement
do...while Statement
for Statement
for...in Statement