no-negated-condition

March 27, 2026 ยท View on GitHub

๐Ÿ“ Disallow negated conditions.

๐Ÿ’ผ This rule is enabled in the following configs: โœ… recommended, โ˜‘๏ธ unopinionated.

๐Ÿ”ง This rule is automatically fixable by the --fix CLI option.

Negated conditions are more difficult to understand. Code can be made more readable by inverting the condition.

This is an improved version of the no-negated-condition ESLint rule that makes it automatically fixable. ESLint did not want to make it fixable.

Examples

// โŒ
if (!a) {
	doSomethingC();
} else {
	doSomethingB();
}

// โœ…
if (a) {
	doSomethingB();
} else {
	doSomethingC();
}
// โŒ
if (a !== b) {
	doSomethingC();
} else {
	doSomethingB();
}

// โœ…
if (a === b) {
	doSomethingB();
} else {
	doSomethingC();
}
// โŒ
!a ? c : b

// โœ…
a ? b : c
// โŒ
if (a != b) {
	doSomethingC();
} else {
	doSomethingB();
}

// โœ…
if (a == b) {
	doSomethingB();
} else {
	doSomethingC();
}
// โœ…
if (!a) {
	doSomething();
}
// โœ…
if (!a) {
	doSomething();
} else if (b) {
	doSomethingElse();
}
// โœ…
if (a != b) {
	doSomething();
}