]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/no-constant-condition.md
8566d8caa65af64384d7a67f9e933815486b3c0e
[pve-eslint.git] / eslint / docs / rules / no-constant-condition.md
1 # disallow constant expressions in conditions (no-constant-condition)
2
3 A constant expression (for example, a literal) as a test condition might be a typo or development trigger for a specific behavior. For example, the following code looks as if it is not ready for production.
4
5 ```js
6 if (false) {
7 doSomethingUnfinished();
8 }
9 ```
10
11 ## Rule Details
12
13 This rule disallows constant expressions in the test condition of:
14
15 * `if`, `for`, `while`, or `do...while` statement
16 * `?:` ternary expression
17
18 Examples of **incorrect** code for this rule:
19
20 ```js
21 /*eslint no-constant-condition: "error"*/
22
23 if (false) {
24 doSomethingUnfinished();
25 }
26
27 if (void x) {
28 doSomethingUnfinished();
29 }
30
31 for (;-2;) {
32 doSomethingForever();
33 }
34
35 while (typeof x) {
36 doSomethingForever();
37 }
38
39 do {
40 doSomethingForever();
41 } while (x = -1);
42
43 var result = 0 ? a : b;
44 ```
45
46 Examples of **correct** code for this rule:
47
48 ```js
49 /*eslint no-constant-condition: "error"*/
50
51 if (x === 0) {
52 doSomething();
53 }
54
55 for (;;) {
56 doSomethingForever();
57 }
58
59 while (typeof x === "undefined") {
60 doSomething();
61 }
62
63 do {
64 doSomething();
65 } while (x);
66
67 var result = x !== 0 ? a : b;
68 ```
69
70 ## Options
71
72 ### checkLoops
73
74 Set to `true` by default. Setting this option to `false` allows constant expressions in loops.
75
76 Examples of **correct** code for when `checkLoops` is `false`:
77
78 ```js
79 /*eslint no-constant-condition: ["error", { "checkLoops": false }]*/
80
81 while (true) {
82 doSomething();
83 if (condition()) {
84 break;
85 }
86 };
87
88 for (;true;) {
89 doSomething();
90 if (condition()) {
91 break;
92 }
93 };
94
95 do {
96 doSomething();
97 if (condition()) {
98 break;
99 }
100 } while (true)
101 ```