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