JavaScript while Statement
JavaScript statement reference manual
Example
The loop in this example continues to run as long as the variable i is less than 5:
var text = "";
var i = 0;
while (i < 5) {
text += "
The number is " + i;
i++;
}
text The output result is:
The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
Try it »
The bottom of this article contains more examples.
Definition and Usage
The while statement executes the loop as long as the specified condition is true.
As long as the specified condition is true, the loop can keep executing the code.
JavaScript supports different types of loops:
- **for **- Loops a code block a certain number of times
- **for/in **- A loop iterates over the properties of an object
- **while** - Loops through a specified block of code while the specified condition is true
- do/while - Also loops through a specified block of code while the specified condition is true, but this statement executes once before the condition is checked
Tip: Use break statement to break out of the loop, usecontinue The statement is used to break out of the current iteration and start the next one.
Browser support
| Statement | |||||
|---|---|---|---|---|---|
| while | Yes | Yes | Yes | Yes | Yes |
Syntax
while (condition) {
code block to be executed
}
Parameter value
| Parameters | Description |
|---|---|
| condition | Required. Defines the condition for executing the loop. If it returns true, the loop continues; if it returns false, the loop stops. |
**注意:** 如果你的条件一直为 true,该循环永远不会结束。该可能导致浏览器崩溃。
** Warning:** If you forget to increment 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
Loops through the array indices and outputs the car names:
var cars = ["BMW", "Volvo", "Saab", "Ford"];
var text = "";
var i = 0;
while (i < cars.length) {
text += cars[i] + "
";
i++;
}
Try it »
Example explanation:
- Implementation: we set the variable before the loop starts (var i = 0;)
- Then, we defined the loop condition. As long as the variable i is less than the length of the array (4)
- Each time the loop runs, 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
The loop starts from the last index of the array:
var cars = ["BMW", "Volvo", "Saab", "Ford"];
var text = "";
var len = cars.length;
while (len--) {
text += cars[len] + "
";
}
Try it »
Example
Using the break statement - exits the loop when the variable i equals 3:
var text = "";
var i = 0;
while (i < 5) {
text += "
The number is " + i;
i++;
if (i == 3) {
break;
}
}
Try it »
Example
Using the continue statement - loops through the code block, skipping that iteration when the variable is "3":
var text = "";
var i = 0;
while (i < 5) {
i++;
if (i == 3) {
continue;
}
text += "
The number is " + i;
}
Try it »
Related pages
JavaScript tutorial: JavaScript While Loop
JavaScript Reference Manual: JavaScript do ... while Statement
JavaScript Reference Manual: JavaScript for Statement
JavaScript Reference Manual: JavaScript break statement
JavaScript Reference Manual: JavaScript continue Statement
JavaScript statement reference manual