JavaScript while statement
JavaScript Statements Reference Manual
Examples
In this case the loop will continue to run as long as the variable i is less than 5 ::
var i = 0;
while (i <5) {
text + = "<br> The number is" + i;
i ++;
}
text output is:
The number is 1
The number is 2
The number is 3
The number is 4
try it"
Bottom of this article contains more examples.
Definition and Usage
while as long as the statement specified condition is true, it will execute a loop.
As long as a specified condition is true, the loop can always execute the code. .
JavaScript support different types of loops:
- for - a certain number of cycles code block
- for / in - loop through the properties of an object
- while - when a specified condition is true cycle specified block of code
- do / the while - the same as the specified condition is true cycle specified code blocks, but the statement will be executed once before the conditional
Tip: Use the break statement to jump out of circulation, the use of continue statement to jump out of the current iteration, and start the next iteration.
Browser Support
Statements | |||||
---|---|---|---|---|---|
while | Yes | Yes | Yes | Yes | Yes |
grammar
code block to be executed
}
Parameter Value
parameter | description |
---|---|
condition | have to. Define the execution condition for the loop. If true, the loop will continue, if it returns false, the cycle stops. Note: If your condition has been true, the cycle never ends. This may cause the browser to crash. NOTE: If you forget to add conditions to the value of the variables used in the cycle never ends. This may cause the browser to crash. |
technical details
JavaScript version: | 1.0 |
---|
More examples
Examples
Cycle through the index of the array, the output car name:
var text = "";
var i = 0;
while (i <cars.length) {
text + = cars [i] + "<br>";
i ++;
}
try it"
Analysis examples:
- Implementation, we set the variable before the start of the cycle (var i = 0;)
- Then, we define what the condition of the loop. Know the variable i is less than the length of the array (4)
- Each time through the loop, the variable is incremented 1 (i ++)
- Once the variables are no longer less than (array length), on the condition is false, the loop is terminated.
Examples
Loop from the last index of the array began:
var text = "";
var len = cars.length;
while (len--) {
text + = cars [len] + "<br>";
}
try it"
Examples
Break statement - the variable i is equal to 3 to exit the loop:
var i = 0;
while (i <5) {
text + = "<br> The number is" + i;
i ++;
if (i == 3) {
break;
}
}
try it"
Examples
Use continue statement - loop code block, skipping the cycles of "3" when the variables are:
var i = 0;
while (i <5) {
i ++;
if (i == 3) {
continue;
}
text + = "<br> The number is" + i;
}
try it"
Related Pages
JavaScript Tutorials: JavaScript the While loop
JavaScript Reference Manual: JavaScript do ... the while statement
JavaScript Reference Manual: JavaScript for statements
JavaScript Reference Manual: JavaScript BREAK statement
JavaScript Reference Manual: JavaScript Continue statement
JavaScript Statements Reference Manual