Strict Equality (===) Required
ID |
javascript.strict_equals |
Severity |
high |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
JavaScript |
Tags |
best-practice, reliability |
Description
Reports the loose-equality operators == and !=, which apply implicit type coercion before comparing. The coercion rules produce many surprising true/false outcomes; almost all of them are bugs. Use === and !==.
The lone idiomatic exception is the x == null (or null == x) shape, which is a common shorthand for "x is null or undefined`". This rule allows that exact shape and flags every other use of `== / !=.
Rationale
-
"" == 0,"0" == false,[] == false,[] == ![]are alltrue. Each one has bitten thousands of developers. -
null == undefinedistruebutnull === undefinedisfalse— the asymmetry hides bugs. -
Type-aware comparisons document intent. Readers and tools (TypeScript, refactoring engines) can reason about typed equality far better than coerced equality.
// Bad - all coerce silently
if (count == "0") { ... }
if (status != 0) { ... }
Remediation
Use === / !==. Keep the x == null shorthand only when you specifically want "null or undefined".
// Good
if (count === 0) { ... }
if (status !== 0) { ... }
// Allowed - null-or-undefined check
if (value == null) { ... }