Previous
For Loop
ABC of JavaScript : A JavaScript Tutorial
While Loop
Next
Functions

While Loop

Syntax :
while(CONDITION) {
BODY
}

This loop will continuasly execute the statements in the body as long as a given condition is true. This loop must be handleed with care. Always make sure that the given condition will be met - or this loop will continue forever. Let us see an example with the while loop that uses while loop in a situation very similler to the one given in the for loop example.

This basically displays all the elements in an array - the same thing we did using the for loop. Notice how the while loop differs from the for loop. In the while loop, only the condition will be given inside the while loops parenthesis - '(' and ')'.


Previous
For Loop
Contents Next
Functions