Papliba

JavaScript Gotchas

"" == 0 and 1 < x < 3

TL;DR


Why "" == 0 is true

"" == 0          // true  (coercion)
"" === 0         // false (no coercion)
Number("") === 0 // true  (explicit)

Rule: Prefer === / !==.


Why 1 < x < 3 is broken

let x = 100;
1 < x < 3   // (true) < 3 → 1 < 3 → true

x = -5;
1 < x < 3   // (false) < 3 → 0 < 3 → true

Correct:

x > 1 && x < 3

Quick Rules


Enable rules to catch these issues.

{
  "rules": {
    "eqeqeq": ["error", "always"],
    "no-implicit-coercion": "error"
  }
}