]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/no-negated-condition.md
ab9e22638767afefca8dfd0ff9044c1636476625
[pve-eslint.git] / eslint / docs / src / rules / no-negated-condition.md
1 ---
2 title: no-negated-condition
3 layout: doc
4 rule_type: suggestion
5 ---
6
7
8 Negated conditions are more difficult to understand. Code can be made more readable by inverting the condition instead.
9
10 ## Rule Details
11
12 This rule disallows negated conditions in either of the following:
13
14 * `if` statements which have an `else` branch
15 * ternary expressions
16
17 Examples of **incorrect** code for this rule:
18
19 ::: incorrect
20
21 ```js
22 /*eslint no-negated-condition: "error"*/
23
24 if (!a) {
25 doSomething();
26 } else {
27 doSomethingElse();
28 }
29
30 if (a != b) {
31 doSomething();
32 } else {
33 doSomethingElse();
34 }
35
36 if (a !== b) {
37 doSomething();
38 } else {
39 doSomethingElse();
40 }
41
42 !a ? c : b
43 ```
44
45 :::
46
47 Examples of **correct** code for this rule:
48
49 ::: correct
50
51 ```js
52 /*eslint no-negated-condition: "error"*/
53
54 if (!a) {
55 doSomething();
56 }
57
58 if (!a) {
59 doSomething();
60 } else if (b) {
61 doSomething();
62 }
63
64 if (a != b) {
65 doSomething();
66 }
67
68 a ? b : c
69 ```
70
71 :::