"" == 0
and 1 < x < 3
===
, not ==
.x > 1 && x < 3
."" == 0
is true
==
coerces types.""
becomes 0
, so it’s 0 == 0
→ true."" == 0 // true (coercion)
"" === 0 // false (no coercion)
Number("") === 0 // true (explicit)
Rule: Prefer ===
/ !==
.
1 < x < 3
is broken1 < x
→ true
or false
true → 1
, false → 0
)1 < 3
or 0 < 3
→ always truelet 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
===
/ !==
.lower < x && x < upper
.const n = Number(s);
Enable rules to catch these issues.
{
"rules": {
"eqeqeq": ["error", "always"],
"no-implicit-coercion": "error"
}
}