This rule raises an issue when a negated expression is directly compared using equality operators (===, !==,
==, !=).
Using a negated expression in an equality check is almost always a logical error that leads to unexpected behavior.
When you write !foo === bar, JavaScript parses this as (!foo) === bar due to operator precedence. This compares the
boolean result of !foo with the value of bar, which is rarely the intended logic.
For example, if foo is "hello" and bar is "world", then !foo === bar becomes
false === "world", which evaluates to false. This is likely not what you wanted to check.
This pattern usually occurs when developers:
foo !== bar but accidentally add the negation operator !(foo === bar) These mistakes can cause conditional statements to behave incorrectly, leading to bugs that are difficult to spot during code review.
This issue can cause logical errors in your application’s control flow. Conditional statements may not work as expected, potentially leading to:
Replace the negated equality check with the correct inequality operator. Use !== instead of !expression === and
=== instead of !expression !==.
if (!foo === bar) { // Noncompliant
// This compares (!foo) with bar
doSomething();
}
if (foo !== bar) {
// This correctly checks if foo is not equal to bar
doSomething();
}