Skip to content

eslint/no-unsafe-negation Correctness

This rule is turned on by default.
🛠️ An auto-fix is available for this rule.

What it does

Disallow negating the left operand of relational operators

Why is this bad?

Just as developers might type -a + b when they mean -(a + b) for the negative of a sum, they might type !key in object by mistake when they almost certainly mean !(key in object) to test that a key is not in an object. !obj instanceof Ctor is similar.

Example

javascript
if ((!key) in object) {
  //operator precedence makes it equivalent to (!key) in object
  //and type conversion makes it equivalent to (key ? "false" : "true") in object
}

References

Released under the MIT License.