]>
Commit | Line | Data |
---|---|---|
8f9d1d4d DC |
1 | --- |
2 | title: for-direction | |
8f9d1d4d DC |
3 | rule_type: problem |
4 | --- | |
5 | ||
6 | ||
eb39fafa DC |
7 | |
8 | ## Rule Details | |
9 | ||
10 | A `for` loop with a stop condition that can never be reached, such as one with a counter that moves in the wrong direction, will run infinitely. While there are occasions when an infinite loop is intended, the convention is to construct such loops as `while` loops. More typically, an infinite for loop is a bug. | |
11 | ||
12 | Examples of **incorrect** code for this rule: | |
13 | ||
8f9d1d4d DC |
14 | :::incorrect |
15 | ||
eb39fafa DC |
16 | ```js |
17 | /*eslint for-direction: "error"*/ | |
18 | for (var i = 0; i < 10; i--) { | |
19 | } | |
20 | ||
21 | for (var i = 10; i >= 0; i++) { | |
22 | } | |
8f9d1d4d DC |
23 | |
24 | for (var i = 0; i > 10; i++) { | |
25 | } | |
eb39fafa DC |
26 | ``` |
27 | ||
8f9d1d4d DC |
28 | ::: |
29 | ||
eb39fafa DC |
30 | Examples of **correct** code for this rule: |
31 | ||
8f9d1d4d DC |
32 | :::correct |
33 | ||
eb39fafa DC |
34 | ```js |
35 | /*eslint for-direction: "error"*/ | |
36 | for (var i = 0; i < 10; i++) { | |
37 | } | |
38 | ``` | |
8f9d1d4d DC |
39 | |
40 | ::: |