unicorn/no-negation-in-equality-check

Disallow ! on the left side of ===, !==, ==, and !=

has suggestions

! binds tighter than ===, so !a === b means (!a) === b. It turns a into true or false and compares that with b. You almost always meant a !== b.

--fix-suggestions removes the ! and flips the operator.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"unicorn/no-negation-in-equality-check" = "error" # or "warn"

Examples#

example.js1 problem
if (!user.role === "admin") {
  deny();
}
example.jsNo problems
if (user.role !== "admin") {
  deny();
}

If you do want to compare the negated value, wrap it in parentheses to say so. !!value, which converts to a boolean, is also allowed:

example.jsNo problems
if ((!isOpen) === wasOpen) {
  toggle();
}

if (!!selected === expected) {
  confirm();
}