JavaScript do/while statement
- Page précédente debugger
- Page suivante for
- Retour au niveau supérieur Manuel de référence instructions JavaScript
Definition and usage
The do/while statement creates a loop that executes the code block once before checking if the condition is true, and then it will repeat the loop as long as the condition is true.
When you want to run the loop at least once, use the do/while statement regardless of how it is.
JavaScript supports different types of loops:
- for - Loop the 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 the code block while the specified condition is true
- do/while - Execute the code block once and then repeat the loop if the specified condition is true
Example
This loop will always execute at least once, even if the condition is false, because the code block is executed before the condition is tested:
var text = ""; var i = 0; do { text += "The number is " + i; i++; } while (i < 5);
Syntax
do { code block to be executed } while (condition);
Parameter value
Parameter | Description |
---|---|
condition |
Required. Define the condition that runs 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 may cause your browser to crash. Note:If you use conditionally variable, please initialize it before the loop and increment it within the loop. If you forget to increase the variable, the loop will never end. This may also cause your browser to crash. |
Technical details
JavaScript version: | ECMAScript 1 |
---|
Browser support
The numbers in the table specify the first browser version that fully supports this phrase.
Phrase | Chrome | IE | Firefox | Safari | Opera |
---|---|---|---|---|---|
do/while | Support | 6.0 | Support | Support | Support |
Pages connexes
Tutoriel JavaScript :Boucle While JavaScript
Manuel de référence JavaScript :Instructions while JavaScript
Manuel de référence JavaScript :Instructions for JavaScript
- Page précédente debugger
- Page suivante for
- Retour au niveau supérieur Manuel de référence instructions JavaScript