You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Loops let a block of code run a certain number of times:
9
10
10
11
```
11
-
// A for loop
12
-
// logs "try 0", "try 1", ..., "try 4"
13
12
for ( var i = 0; i < 5; i++ ) {
14
-
13
+
// Logs "try 0", "try 1", ..., "try 4".
15
14
console.log( "try " + i );
16
-
17
15
}
18
16
```
19
17
20
-
Note that in loops, the variable i is not "scoped" to the loop block even though the keyword `var` is used before the variable name. Scope is covered in more depth in the [Scope](/scope/) section.
18
+
Note that in loops, the variable `i` is not "scoped" to the loop block even though the keyword `var` is used before the variable name. Scope is covered in more depth in the [Scope](/scope/) section.
21
19
22
-
## The `for`loop
20
+
## The `for`Loop
23
21
24
22
A `for` loop is made up of four statements and has the following structure:
25
23
26
24
```
27
25
for ( [initialisation]; [conditional]; [iteration] ) {
28
26
29
-
[loopBody]
27
+
[loopBody]
30
28
31
29
}
32
30
```
@@ -37,22 +35,21 @@ The _conditional_ statement is executed before each iteration, and its return va
37
35
38
36
The _iteration_ statement is executed at the end of each iteration and gives you an opportunity to change the state of important variables. Typically, this will involve incrementing or decrementing a counter and thus bringing the loop closer to its end.
39
37
40
-
The _loopBody_ statement is what runs on every iteration. It can contain anything. Typically, there will be multiple statements that need to be executed, and should be wrapped in a block ({...}).
38
+
The _loopBody_ statement is what runs on every iteration. It can contain anything. Typically, there will be multiple statements that need to be executed, and should be wrapped in a block (`{...}`).
41
39
42
40
Here's a typical `for` loop:
43
41
44
42
```
45
-
// A typical for loop
46
43
for (var i = 0, limit = 100; i < limit; i++) {
47
-
// This block will be executed 100 times
48
-
console.log( 'Currently at ' + i );
49
-
// Note: the last log will be "Currently at 99"
44
+
// This block will be executed 100 times.
45
+
console.log( "Currently at " + i );
46
+
// Note: The last log will be "Currently at 99".
50
47
}
51
48
```
52
49
53
50
## The `while` loop
54
51
55
-
A while loop is similar to an `if` statement, except that its body will keep executing until the condition evaluates to false.
52
+
A while loop is similar to an `if` statement, except that its body will keep executing until the condition evaluates to a falsy value.
56
53
57
54
```
58
55
while ( [conditional] ) {
@@ -65,45 +62,41 @@ while ( [conditional] ) {
65
62
Here's a typical `while` loop:
66
63
67
64
```
68
-
// A typical while loop
69
65
var i = 0;
70
66
while ( i < 100 ) {
71
-
// This block will be executed 100 times
67
+
// This block will be executed 100 times.
72
68
console.log( "Currently at " + i );
73
-
74
-
// increment i
75
-
i++;
69
+
i++; // Increment i
76
70
}
77
71
```
78
72
79
73
Notice that the counter is incrementing within the loop's body. It's possible to combine the conditional and incrementer, like so:
80
74
81
75
```
82
-
// A while loop with a combined conditional and incrementer
83
76
var i = -1;
84
77
while ( ++i < 100 ) {
85
-
// This block will be executed 100 times
78
+
// This block will be executed 100 times.
86
79
console.log( "Currently at " + i );
87
80
}
88
81
```
89
82
90
-
Notice that the counter starts at -1 and uses the prefix incrementer (++i).
83
+
Notice that the counter starts at -1 and uses the prefix incrementer (`++i`).
91
84
92
-
## The `do-while`loop
85
+
## The `do-while`Loop
93
86
94
87
This is almost exactly the same as the `while` loop, except for the fact that the loop's body is executed at least once before the condition is tested.
95
88
96
89
```
97
90
do {
98
91
99
-
[loopBody]
92
+
[loopBody]
100
93
101
94
} while ( [conditional] )
102
95
```
96
+
103
97
Here's a `do-while` loop:
104
98
105
99
```
106
-
// A do-while loop
107
100
do {
108
101
// Even though the condition evaluates to false
109
102
// this loop's body will still execute once.
@@ -114,9 +107,9 @@ do {
114
107
115
108
These types of loops are quite rare since only few situations require a loop that blindly executes at least once. Regardless, it's good to be aware of it.
116
109
117
-
## Breaking and continuing
110
+
## Breaking and Continuing
118
111
119
-
Usually, a loop's termination will result from the conditional statement not evaluating to true, but it is possible to stop a loop in its tracks from within the loop's body with the `break` statement.
112
+
Usually, a loop's termination will result from the conditional statement not evaluating to a truthy value, but it is possible to stop a loop in its tracks from within the loop's body with the `break` statement:
120
113
121
114
```
122
115
// Stopping a loop
@@ -127,7 +120,7 @@ for ( var i = 0; i < 10; i++ ) {
127
120
}
128
121
```
129
122
130
-
You may also want to continue the loop without executing more of the loop's body. This is done using the `continue` statement.
123
+
You may also want to continue the loop without executing more of the loop's body. This is done using the `continue` statement:
131
124
132
125
```
133
126
// Skipping to the next iteration of a loop
@@ -137,7 +130,7 @@ for ( var i = 0; i < 10; i++ ) {
137
130
}
138
131
139
132
// The following statement will only be executed
140
-
// if the conditional 'something' has not been met
133
+
// if the conditional "something" has not been met
0 commit comments