Skip to main content

no-constant-binary-expression

Comparisons that always evaluate to true or false, and logical expressions that either always short-circuit or never short-circuit, are usually programmer errors.
Rule Type: Problem
Fixable: No

Why This Rule Exists

These errors are especially common in complex expressions where operator precedence is easy to misjudge:
Also, comparing newly constructed objects by reference will always be false, which can surprise developers from other languages:

Rule Details

This rule identifies:
  • == and === comparisons that always evaluate to true or false
  • ||, &&, and ?? expressions that always or never short-circuit

Examples

Incorrect Code

Correct Code

Common Patterns

Object/Array Comparisons

In JavaScript, objects are compared by reference, not by value. A new object never equals any other value.

Type Coercion Issues

Operator Precedence

Constructor vs Function

When Not to Use It

This rule has no legitimate use cases for disabling - constant expressions are always mistakes. If you need to disable it for a specific line, you may have found a false positive bug in ESLint.

Further Reading