Skip to main content

No boolean literal compare

Has auto-fix

Rule id

no-boolean-literal-compare

Severity

Style

Description

Warns on comparison to a boolean literal, as in x == true. Comparing boolean values to boolean literals is unnecessary, as those expressions will result in booleans too. Just use the boolean values directly or negate them.

Unnecessary comparing

Bad:

  var b = x == true; // LINT
var c = x != true; // LINT

// LINT
if (x == true) {
...
}

// LINT
if (x != false) {
...
}

Good:

  var b = x;
var c = !x;

if (x) {
...
}

if (!x) {
...
}