JavaScript whileStatement

JavaScript 语句参考手册JavaScript Statement Reference

Example

The loop in this example will continue to run as long as the variable i is less than 5:

var text = "";
var i = 0;
while (i < 5) {
    text += "<br>The number is " + i;
    i++;
}

textOutput:

The number is 0
The number is 1
The number is 2
The number is 3
The number is 4

Try it Yourself »

More examples are included at the bottom of this article.


Definition and Usage

The while statement executes a loop as long as a specified condition is true.

As long as the specified condition is true, the loop can continue to execute code.

JavaScript supports different types of loops:

  • for - Loop a block of code a certain number of times
  • for/in - Loop through the properties of an object
  • while - Loop through a specified block of code when a specified condition is true
  • do/while- Also loops through a specified block of code when a specified condition is true, but this statement executes once before the condition is tested

Tip:Usebreakstatement to break out of the loop, usecontinuestatement to skip the current iteration and continue with the next iteration.


Browser Support

Statement
while Yes Yes Yes Yes Yes


Syntax

while (condition) {
    code block to be executed
}

Parameter Values

Parameter Description
condition Required. Defines the condition for executing the loop. If it returns true, the loop continues; if it returns false, the loop stops.

Note:If your condition is always true, the loop will never end. This may cause the browser to crash.

Note:If you forget to increase the value of the variable used in the condition, the loop will never end. This may cause the browser to crash.

Technical Details

JavaScript Version: 1.0


实例

More Examples

Example

Loop through the indexes of an array, and output the car names:

var cars = ["BMW", "Volvo", "Saab", "Ford"];
var text = "";
var i = 0;
while (i < cars.length) {
    text += cars[i] + "<br>";
    i++;
}

Try it Yourself »

Example explained:

  • Implementation: we set a variable before the loop starts (var i = 0;)
  • Then, we define the condition for the loop. It runs until the variable i is less than the array length (4)
  • Each time the loop executes, the variable increments by 1 (i++)
  • Once the variable is no longer less than (the array length), the condition becomes false, and the loop terminates.

Example

Loop starting from the last index of the array:

var cars = ["BMW", "Volvo", "Saab", "Ford"];
var text = "";
var len = cars.length;
while (len--) {
    text += cars[len] + "<br>";
}

Try it Yourself »

Example

Use the break statement - exit the loop when the variable i equals 3:

var text = "";
var i = 0;
while (i < 5) {
    text += "<br>The number is " + i;
    i++;
    if (i == 3) {
        break;
    }
}

Try it Yourself »

Example

Use the continue statement - loop through a block of code, skipping the iteration when the variable is "3":

var text = "";
var i = 0;
while (i < 5) {
    i++;
    if (i == 3) {
        continue;
    }
text += "<br>The number is " + i;
}

Try it Yourself »


Related Pages

JavaScript Tutorial:JavaScript While Loop

JavaScript Reference:JavaScript do ... while Statement

JavaScript Reference:JavaScript for Statement

JavaScript Reference:JavaScript break Statement

JavaScript Reference:JavaScript continue Statement


JavaScript 语句参考手册JavaScript Statement Reference Other Extensions