Skip to main content

avoid-inverted-boolean-checks

added in: 1.4.0

Warns when a condition has an inverted check.

Inverted checks are harder to notice due to one symbol ! that completely inverts the condition. Refactoring the code to be without the inverted check helps other developers to understand it faster.

Example

❌ Bad:

if (!(x == 1)) {} // LINT

if (!(x != 1)) {} // LINT

if (!(x > 1)) {} // LINT

if (!(x < 1)) {} // LINT

if (!(x >= 1)) {} // LINT

if (!(x <= 1)) {} // LINT

var b = !(x != 1) ? 1 : 2; // LINT

var foo = !(x <= 4); // LINT

function(!(x == 1)); // LINT

if (!(a > 4 && b < 2)) {} // LINT

✅ Good:

if (x != 1) {}

if (x == 1) {}

if (x <= 1) {}

if (x >= 1) {}

if (x < 1) {}

if (x > 1) {}

var b = x == 1 ? 1 : 2;

var foo = x > 4;

function(x != 1);

if (a <= 4 || b >= 2) {}