This rule raises an issue when a negated expression is directly compared using equality operators (===, !==, ==, !=).

Why is this an issue?

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:

These mistakes can cause conditional statements to behave incorrectly, leading to bugs that are difficult to spot during code review.

What is the potential impact?

This issue can cause logical errors in your application’s control flow. Conditional statements may not work as expected, potentially leading to:

How to fix?

Replace the negated equality check with the correct inequality operator. Use !== instead of !expression === and === instead of !expression !==.

Non-compliant code example

if (!foo === bar) { // Noncompliant
  // This compares (!foo) with bar
  doSomething();
}

Compliant code example

if (foo !== bar) {
  // This correctly checks if foo is not equal to bar
  doSomething();
}

Documentation