Disallow negated conditions
Negated conditions are more difficult to understand. Code can be made more readable by inverting the condition instead.
This rule disallows negated conditions in either of the following:
if
statements which have an else
branchExamples of incorrect code for this rule:
/*eslint no-negated-condition: "error"*/
if (!a) {
doSomething();
} else {
doSomethingElse();
}
if (a != b) {
doSomething();
} else {
doSomethingElse();
}
if (a !== b) {
doSomething();
} else {
doSomethingElse();
}
!a ? c : b
Examples of correct code for this rule:
/*eslint no-negated-condition: "error"*/
if (!a) {
doSomething();
}
if (!a) {
doSomething();
} else if (b) {
doSomething();
}
if (a != b) {
doSomething();
}
a ? b : c
This rule was introduced in ESLint v1.6.0.
© OpenJS Foundation and other contributors
Licensed under the MIT License.
https://eslint.org/docs/latest/rules/no-negated-condition