-
Notifications
You must be signed in to change notification settings - Fork 22.4k
/
index.md
140 lines (108 loc) · 4.17 KB
/
index.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
---
title: do...while
slug: Web/JavaScript/Reference/Statements/do...while
page-type: javascript-statement
browser-compat: javascript.statements.do_while
---
{{jsSidebar("Statements")}}
The **`do...while`** statement creates a loop that executes a specified statement as long as the test condition evaluates to true. The condition is evaluated after executing the statement, resulting in the specified statement executing at least once.
{{EmbedInteractiveExample("pages/js/statement-dowhile.html")}}
## Syntax
```js-nolint
do
statement
while (condition);
```
- `statement`
- : A statement that is executed at least once and re-executed as long as the condition evaluates to true. You can use a [block statement](/en-US/docs/Web/JavaScript/Reference/Statements/block) to execute multiple statements.
- `condition`
- : An expression evaluated _after_ each pass through the loop. If this condition [evaluates to true](/en-US/docs/Glossary/Truthy), `statement` is re-executed. When condition [evaluates to false](/en-US/docs/Glossary/Falsy), execution continues with the statement after the `do...while` loop.
## Description
Like other looping statements, you can use [control flow statements](/en-US/docs/Web/JavaScript/Reference/Statements#control_flow) inside `statement`:
- {{jsxref("Statements/break", "break")}} stops `statement` execution and goes to the first statement after the loop.
- {{jsxref("Statements/continue", "continue")}} stops `statement` execution and re-evaluates `condition`.
The `do...while` statement syntax requires a semicolon at the end, but the [automatic semicolon insertion](/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#automatic_semicolon_insertion) process may insert one for you if the lack of a semicolon results in invalid syntax.
## Examples
### Using do...while
In the following example, the `do...while` loop iterates at least once and
reiterates until `i` is no longer less than 5.
```js
let result = "";
let i = 0;
do {
i += 1;
result += `${i} `;
} while (i > 0 && i < 5);
// Despite i === 0 this will still loop as it starts off without the test
console.log(result);
```
### Using false as do...while condition
Because the statement is always executed once, `do...while (false)` is the same as executing the statement itself. This is a common idiom in C-like languages, which allows you to use `break` to break out of branching logic early.
```js
do {
if (!user.loggedIn) {
console.log("You are not logged in");
break;
}
const friends = user.getFriends();
if (!friends.length) {
console.log("No friends found");
break;
}
for (const friend of friends) {
handleFriend(friend);
}
} while (false);
// The rest of code
```
In JavaScript, there are some alternatives, such as using a [labeled block statement](/en-US/docs/Web/JavaScript/Reference/Statements/label) with `break`:
```js
handleFriends: {
if (!user.loggedIn) {
console.log("You are not logged in");
break handleFriends;
}
const friends = user.getFriends();
if (!friends.length) {
console.log("No friends found");
break handleFriends;
}
for (const friend of friends) {
handleFriend(friend);
}
}
```
Or using a function:
```js
function handleFriends() {
if (!user.loggedIn) {
console.log("You are not logged in");
return;
}
const friends = user.getFriends();
if (!friends.length) {
console.log("No friends found");
return;
}
for (const friend of friends) {
handleFriend(friend);
}
}
```
### Using an assignment as a condition
In some cases, it can make sense to use an assignment as a condition, such as this:
```js
do {
// …
} while ((match = regexp.exec(str)));
```
But when you do, there are readability tradeoffs. The [`while`](/en-US/docs/Web/JavaScript/Reference/Statements/while) documentation has a [Using an assignment as a condition](/en-US/docs/Web/JavaScript/Reference/Statements/while#using_an_assignment_as_a_condition) section with our recommendations.
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- {{jsxref("Statements/while", "while")}}
- {{jsxref("Statements/for", "for")}}
- {{jsxref("Statements/break", "break")}}
- {{jsxref("Statements/continue", "continue")}}