JavaScript while statement
- Previous Page var
- Next Page break
- Go to Parent Directory JavaScript Statement Reference Manual
Definition and usage
The while statement creates a loop that executes when the specified condition is true.
The loop will continue to run as long as the condition is true. It will only stop when the condition becomes false.
JavaScript supports different types of loops:
- for - Loop through a code block multiple times
- for/in - Loop through the properties of an object
- for/of - Loop through the values of an iterable object
- while - Loop through a code block when the specified condition is true
- do/while - Execute a code block once and then repeat the loop when the specified condition is true
Tip:Use the break statement to exit the loop, and use the continue statement to skip a value within the loop.
Example
The loop block will repeat 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++; }
More TIY examples are below the page.
Syntax
while (condition) { code block to be executed }
Parameter Value
Parameter | Description |
---|---|
condition |
Required. Defines the condition for running the loop (code block). If it returns true, the loop will restart, and if it returns false, the loop will end. Note:If the condition is always true, the loop will never end. This will crash your browser. Note:If you use a variable with a condition, initialize it before the loop and increment it within the loop. If you forget to increase the variable, the loop will never end. This will also crash your browser. |
Technical Details
JavaScript Version: | ECMAScript 1 |
---|
More Examples
Example
Loop through the indices of the array to collect car names from the cars array:
var cars = ["BMW", "Volvo", "Saab", "Ford"]; var text = ""; var i = 0; while (i < cars.length) { text += cars[i] + "<br>"; i++; }
Example Explanation:
- First, we set a variable before the loop starts (var i = 0;)
- First, we define the condition for the loop to run. The loop will continue as long as the variable is less than the length of the array (i.e., 4)
- The variable is incremented by one (i++) each time the loop executes
- Once the variable is no longer less than 4 (the length of the array), the condition is false, and the loop ends
Example
Loop through array indices backward:
var cars = ["BMW", "Volvo", "Saab", "Ford"]; var text = ""; var len = cars.length; while (len--) { text += cars[len] + "<br>"; }
Example
Use the break statement - within a loop block, but 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; } }
Example
Use the continue statement - within a loop block, but skip the value "3":
var text = ""; var i = 0; while (i < 5) { i++; if (i == 3) { continue; } text += "<br>The number is " + i; }
Browser Support
Statement | Chrome | IE | Firefox | Safari | Opera |
---|---|---|---|---|---|
while | Supported | Supported | Supported | Supported | Supported |
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
- Previous Page var
- Next Page break
- Go to Parent Directory JavaScript Statement Reference Manual