while
statement
JS Home <<
JS Reference <<
while
Creates a loop structure that may or may not be executed.
Description
Creates a loop through a section of code which will be executed while a condition remains true. If the condition is false to begin with the loop doesn't execute.
Syntax
Signature | Description |
---|---|
while (condition) | Creates a loop through a section of code which will be executed while a condition remains true.
|
Parameters
Parameter | Description |
---|---|
condition | An expression that evaluates to true or false and is tested before each pass through the loop. |
statement | Executed when expression evaluates to true . |
Examples
The code below shows some while
loops.
// execute the loop till a condition met.
var aVariable = 1;
while (aVariable < 10) {
alert('aVariable = ' + aVariable);
aVariable += 4;
}
alert('left first while loop');
// this loop will never execute.
var bVariable = 11;
while (bVariable < 10) {
alert('bVariable = ' + bVariable);
bVariable += 4;
}
alert('left second while loop');
Related Tutorials
JavaScript Intermediate Tutorials - Lesson 4 - While and Do....While Loops