Repository navigation
Expand file tree
/
Copy pathindex.md
More file actions
457 lines (359 loc) · 13.2 KB
/
Copy pathindex.md
File metadata and controls
457 lines (359 loc) · 13.2 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
---
title: Loops and iteration
slug: Web/JavaScript/Guide/Loops_and_iteration
page-type: guide
sidebar: jssidebar
---
{{PreviousNext("Web/JavaScript/Guide/Control_flow_and_error_handling", "Web/JavaScript/Guide/Functions")}}
Loops offer a quick and easy way to do something repeatedly. This
chapter of the [JavaScript Guide](/en-US/docs/Web/JavaScript/Guide)
introduces the different iteration statements available to JavaScript.
You can think of a loop as a computerized version of the game where you tell someone to
take _X_ steps in one direction, then _Y_ steps in another. For example,
the idea "Go five steps to the east" could be expressed this way as a loop:
```js
for (let step = 0; step < 5; step++) {
// Runs 5 times, with values of step 0 through 4.
console.log("Walking east one step");
}
```
There are many different kinds of loops, but they all essentially do the same thing:
they repeat an action some number of times. (Note that it's possible that number could
be zero!)
The various loop mechanisms offer different ways to determine the start and end points
of the loop. There are various situations that are more easily served by one type of
loop over the others.
The statements for loops provided in JavaScript are:
- [for statement](#for_statement)
- [do...while statement](#do...while_statement)
- [while statement](#while_statement)
- [labeled statement](#labeled_statement)
- [break statement](#break_statement)
- [continue statement](#continue_statement)
- [for...in statement](#for...in_statement)
- [for...of statement](#for...of_statement)
## for statement
A {{jsxref("Statements/for", "for")}} loop repeats until a specified condition evaluates to false. The JavaScript `for` loop is similar to the Java and C `for` loop.
A `for` statement looks as follows:
```js-nolint
for (initialization; condition; afterthought)
statement
```
When a `for` loop executes, the following occurs:
1. The initializing expression `initialization`, if any, is executed. This expression usually initializes one or more loop counters, but the syntax allows an expression of any degree of complexity. This expression can also declare variables.
2. The `condition` expression is evaluated. If the value of `condition` is true, the loop statements execute. Otherwise, the `for` loop terminates. (If the `condition` expression is omitted entirely, the condition is assumed to be true.)
3. The `statement` executes. To execute multiple statements, use a [block statement](/en-US/docs/Web/JavaScript/Reference/Statements/block) (`{ }`) to group those statements.
4. If present, the update expression `afterthought` is executed.
5. Control returns to Step 2.
### Example
In the example below, the function contains a `for` statement that counts
the number of selected options in a scrolling list (a [`<select>`](/en-US/docs/Web/HTML/Reference/Elements/select)
element that allows multiple selections).
#### HTML
```html
<form name="selectForm">
<label for="musicTypes"
>Choose some music types, then click the button below:</label
>
<select id="musicTypes" name="musicTypes" multiple>
<option selected>R&B</option>
<option>Jazz</option>
<option>Blues</option>
<option>New Age</option>
<option>Classical</option>
<option>Opera</option>
</select>
<button id="btn" type="button">How many are selected?</button>
</form>
```
#### JavaScript
Here, the `for` statement declares the variable `i` and initializes it to `0`. It checks that `i` is less than the number of options in the `<select>` element, performs the succeeding `if` statement, and increments `i` by 1 after each pass through the loop.
```js
function countSelected(selectObject) {
let numberSelected = 0;
for (let i = 0; i < selectObject.options.length; i++) {
if (selectObject.options[i].selected) {
numberSelected++;
}
}
return numberSelected;
}
const btn = document.getElementById("btn");
btn.addEventListener("click", () => {
const musicTypes = document.selectForm.musicTypes;
console.log(`You have selected ${countSelected(musicTypes)} option(s).`);
});
```
## do...while statement
The {{jsxref("Statements/do...while", "do...while")}} statement repeats until a
specified condition evaluates to false.
A `do...while` statement looks as follows:
```js-nolint
do
statement
while (condition);
```
`statement` is always executed once before the condition is
checked. (To execute multiple statements, use a block statement (`{ }`)
to group those statements.)
If `condition` is `true`, the statement executes again. At the
end of every execution, the condition is checked. When the condition is
`false`, execution stops, and control passes to the statement following
`do...while`.
### Example
In the following example, the `do` loop iterates at least once and
reiterates until `i` is no longer less than `5`.
```js
let i = 0;
do {
i += 1;
console.log(i);
} while (i < 5);
```
## while statement
A {{jsxref("Statements/while", "while")}} statement executes its statements as long as a
specified condition evaluates to `true`. A `while` statement looks
as follows:
```js-nolint
while (condition)
statement
```
If the `condition` becomes `false`,
`statement` within the loop stops executing and control passes to the
statement following the loop.
The condition test occurs _before_ `statement` in the loop is
executed. If the condition returns `true`, `statement` is executed
and the `condition` is tested again. If the condition returns
`false`, execution stops, and control is passed to the statement following
`while`.
To execute multiple statements, use a block statement (`{ }`) to group
those statements.
### Example 1
The following `while` loop iterates as long as `n` is
less than `3`:
```js
let n = 0;
let x = 0;
while (n < 3) {
n++;
x += n;
}
```
With each iteration, the loop increments `n` and adds that value to
`x`. Therefore, `x` and `n` take on the following
values:
- After the first pass: `n` = `1` and `x` =
`1`
- After the second pass: `n` = `2` and `x` =
`3`
- After the third pass: `n` = `3` and `x` =
`6`
After completing the third pass, the condition `n < 3` is no longer
`true`, so the loop terminates.
### Example 2
Avoid infinite loops. Make sure the condition in a loop eventually becomes
`false`—otherwise, the loop will never terminate! The statements in the
following `while` loop execute forever because the condition never becomes
`false`:
```js example-bad
// Infinite loops are bad!
while (true) {
console.log("Hello, world!");
}
```
## labeled statement
A {{jsxref("Statements/label", "label")}} provides a statement with an identifier that
lets you refer to it elsewhere in your program. For example, you can use a label to
identify a loop, and then use the `break` or `continue` statements
to indicate whether a program should interrupt the loop or continue its execution.
The syntax of the labeled statement looks like the following:
```js-nolint
label:
statement
```
The value of `label` may be any JavaScript identifier that is not a
reserved word. The `statement` that you identify with a label may be
any statement. For examples of using labeled statements, see the examples of `break` and `continue` below.
## break statement
Use the {{jsxref("Statements/break", "break")}} statement to terminate a loop,
`switch`, or in conjunction with a labeled statement.
- When you use `break` without a label, it terminates the innermost
enclosing `while`, `do-while`, `for`, or
`switch` immediately and transfers control to the following statement.
- When you use `break` with a label, it terminates the specified labeled
statement.
The syntax of the `break` statement looks like this:
```js-nolint
break;
break label;
```
1. The first form of the syntax terminates the innermost enclosing loop or `switch`.
2. The second form of the syntax terminates the specified enclosing labeled statement.
### Example 1
The following example iterates through the elements in an array until it finds the
index of an element whose value is `theValue`:
```js
for (let i = 0; i < a.length; i++) {
if (a[i] === theValue) {
break;
}
}
```
### Example 2: Breaking to a label
```js
let x = 0;
let z = 0;
labelCancelLoops: while (true) {
console.log("Outer loops:", x);
x += 1;
z = 1;
while (true) {
console.log("Inner loops:", z);
z += 1;
if (z === 10 && x === 10) {
break labelCancelLoops;
} else if (z === 10) {
break;
}
}
}
```
## continue statement
The {{jsxref("Statements/continue", "continue")}} statement can be used to restart a
`while`, `do-while`, `for`, or `label`
statement.
- When you use `continue` without a label, it terminates the current
iteration of the innermost enclosing `while`, `do-while`, or
`for` statement and continues execution of the loop with the next
iteration. In contrast to the `break` statement, `continue` does
not terminate the execution of the loop entirely. In a `while` loop, it
jumps back to the condition. In a `for` loop, it jumps to the
`increment-expression`.
- When you use `continue` with a label, it applies to the looping statement
identified with that label.
The syntax of the `continue` statement looks like the following:
```js-nolint
continue;
continue label;
```
### Example 1
The following example shows a `while` loop with a `continue`
statement that executes when the value of `i` is `3`. Thus,
`n` takes on the values `1`, `3`, `7`, and
`12`.
```js
let i = 0;
let n = 0;
while (i < 5) {
i++;
if (i === 3) {
continue;
}
n += i;
console.log(n);
}
// Logs:
// 1 3 7 12
```
If you comment out the `continue;`, the loop would run till the end and you would see `1,3,6,10,15`.
### Example 2
A statement labeled `checkIandJ` contains a statement labeled
`checkJ`. If `continue` is encountered, the program
terminates the current iteration of `checkJ` and begins the next
iteration. Each time `continue` is encountered, `checkJ`
reiterates until its condition returns `false`. When `false` is
returned, the remainder of the `checkIandJ` statement is completed,
and `checkIandJ` reiterates until its condition returns
`false`. When `false` is returned, the program continues at the
statement following `checkIandJ`.
If `continue` had a label of `checkIandJ`, the program
would continue at the top of the `checkIandJ` statement.
```js
let i = 0;
let j = 10;
checkIandJ: while (i < 4) {
console.log(i);
i += 1;
checkJ: while (j > 4) {
console.log(j);
j -= 1;
if (j % 2 === 0) {
continue;
}
console.log(j, "is odd.");
}
console.log("i =", i);
console.log("j =", j);
}
```
## for...in statement
The {{jsxref("Statements/for...in", "for...in")}} statement iterates a specified
variable over all the enumerable properties of an object. For each distinct property,
JavaScript executes the specified statements. A `for...in` statement looks as
follows:
```js-nolint
for (variable in object)
statement
```
### Example
The following function takes as its argument an object and the object's name. It then
iterates over all the object's properties and returns a string that lists the property
names and their values.
```js
function dumpProps(obj, objName) {
let result = "";
for (const i in obj) {
result += `${objName}.${i} = ${obj[i]}<br>`;
}
result += "<hr>";
return result;
}
```
For an object `car` with properties `make` and `model`, `result` would be:
```plain
car.make = Ford
car.model = Mustang
```
### Arrays
Although it may be tempting to use this as a way to iterate over {{jsxref("Array")}}
elements, the `for...in` statement will return the name of your user-defined
properties in addition to the numeric indexes.
Therefore, it is better to use a traditional {{jsxref("Statements/for", "for")}} loop
with a numeric index when iterating over arrays, because the `for...in`
statement iterates over user-defined properties in addition to the array elements, if
you modify the Array object (such as adding custom properties or methods).
## for...of statement
The {{jsxref("Statements/for...of", "for...of")}} statement creates a loop Iterating
over [iterable objects](/en-US/docs/Web/JavaScript/Reference/Iteration_protocols) (including
{{jsxref("Array")}}, {{jsxref("Map")}}, {{jsxref("Set")}},
{{jsxref("Functions/arguments", "arguments")}} object and so on), invoking a custom
iteration hook with statements to be executed for the value of each distinct property.
```js-nolint
for (variable of iterable)
statement
```
The following example shows the difference between a `for...of` loop and a
{{jsxref("Statements/for...in", "for...in")}} loop. While `for...in` iterates
over property names, `for...of` iterates over property values:
```js
const arr = [3, 5, 7];
arr.foo = "hello";
for (const i in arr) {
console.log(i);
}
// "0" "1" "2" "foo"
for (const i of arr) {
console.log(i);
}
// Logs: 3 5 7
```
The `for...of` and `for...in` statements can also be used with [destructuring](/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring). For example, you can simultaneously loop over the keys and values of an object using {{jsxref("Object.entries()")}}.
```js
const obj = { foo: 1, bar: 2 };
for (const [key, val] of Object.entries(obj)) {
console.log(key, val);
}
// "foo" 1
// "bar" 2
```
{{PreviousNext("Web/JavaScript/Guide/Control_flow_and_error_handling", "Web/JavaScript/Guide/Functions")}}