Skip to content

Commit 43ceab0

Browse files
Markus Amalthea Magnusonajpiano
Markus Amalthea Magnuson
authored andcommitted
Various small fixes on the JavaScript 101 Loops page. Fixes jquery#351.
1 parent 120b66d commit 43ceab0

File tree

1 file changed

+21
-28
lines changed

1 file changed

+21
-28
lines changed

page/javascript-101/loops.md

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,26 @@ source: http://jqfundamentals.com/legacy
55
attribution:
66
- jQuery Fundamentals
77
---
8+
89
Loops let a block of code run a certain number of times:
910

1011
```
11-
// A for loop
12-
// logs "try 0", "try 1", ..., "try 4"
1312
for ( var i = 0; i < 5; i++ ) {
14-
13+
// Logs "try 0", "try 1", ..., "try 4".
1514
console.log( "try " + i );
16-
1715
}
1816
```
1917

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.
2119

22-
## The `for` loop
20+
## The `for` Loop
2321

2422
A `for` loop is made up of four statements and has the following structure:
2523

2624
```
2725
for ( [initialisation]; [conditional]; [iteration] ) {
2826
29-
[ loopBody ]
27+
[loopBody]
3028
3129
}
3230
```
@@ -37,22 +35,21 @@ The _conditional_ statement is executed before each iteration, and its return va
3735

3836
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.
3937

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 (`{...}`).
4139

4240
Here's a typical `for` loop:
4341

4442
```
45-
// A typical for loop
4643
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".
5047
}
5148
```
5249

5350
## The `while` loop
5451

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.
5653

5754
```
5855
while ( [conditional] ) {
@@ -65,45 +62,41 @@ while ( [conditional] ) {
6562
Here's a typical `while` loop:
6663

6764
```
68-
// A typical while loop
6965
var i = 0;
7066
while ( i < 100 ) {
71-
// This block will be executed 100 times
67+
// This block will be executed 100 times.
7268
console.log( "Currently at " + i );
73-
74-
// increment i
75-
i++;
69+
i++; // Increment i
7670
}
7771
```
7872

7973
Notice that the counter is incrementing within the loop's body. It's possible to combine the conditional and incrementer, like so:
8074

8175
```
82-
// A while loop with a combined conditional and incrementer
8376
var i = -1;
8477
while ( ++i < 100 ) {
85-
// This block will be executed 100 times
78+
// This block will be executed 100 times.
8679
console.log( "Currently at " + i );
8780
}
8881
```
8982

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`).
9184

92-
## The `do-while` loop
85+
## The `do-while` Loop
9386

9487
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.
9588

9689
```
9790
do {
9891
99-
[ loopBody ]
92+
[loopBody]
10093
10194
} while ( [conditional] )
10295
```
96+
10397
Here's a `do-while` loop:
10498

10599
```
106-
// A do-while loop
107100
do {
108101
// Even though the condition evaluates to false
109102
// this loop's body will still execute once.
@@ -114,9 +107,9 @@ do {
114107

115108
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.
116109

117-
## Breaking and continuing
110+
## Breaking and Continuing
118111

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:
120113

121114
```
122115
// Stopping a loop
@@ -127,7 +120,7 @@ for ( var i = 0; i < 10; i++ ) {
127120
}
128121
```
129122

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:
131124

132125
```
133126
// Skipping to the next iteration of a loop
@@ -137,7 +130,7 @@ for ( var i = 0; i < 10; i++ ) {
137130
}
138131
139132
// 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
141134
console.log( "I have been reached" );
142135
143136
}

0 commit comments

Comments
 (0)