JavaScript While Loop

Last Updated Jul 29, 2015, 07:00:14 PM

JavaScript While Loop

The while statement creates a loop that executes a specified statement as long as the test condition evaluates to true. The condition is evaluated before executing the statement.




An expression evaluated before each pass through the loop. If this condition evaluates to true, a statement is executed. When condition evaluates to false, execution continues with the statement after the while loop.


A statement that is executed as long as the condition evaluates to true. To execute multiple statements within the loop, use a block statement ({ ... }) to group those statements.


Each iteration, the loop increments n and adds it to m. Therefore, m and n take on the following values:

After the first pass: n = 1 and m = 1

After the second pass: n = 2 and m = 3

After the third pass: n = 3 and m = 6

After the third pass: n = 4 and m = 12

After completing the third pass, the condition n < 10 is no longer true, so the loop terminates.

Another JavaSript While loop Example

Try It Now

While loop with Strings

Try It Now

Printing All Elements in Array using JavaScript While loop

Try It Now

Other JavaScript conditions you might want to learn
JavaScript If Statement

JavaScript if else Statement

JavaScript switchcase Statement

Browser compatibility

while loop Yes Yes Yes Yes Yes Yes Yes

Practice with out Interactive Editor and take your JavaScript Skills to the next level

Exercise 1 Exercise 2 Exercise 3 Exercise 4 Exercise 5 Exercise 6

Sources and Credits

The source of the content has been referred and updated with Mozilla Developer Network and W3C Organization

Last Updated Jul 29, 2015, 07:00:14 PM