Compare Against Negative Zero
ID |
javascript.no_compare_neg_zero |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
JavaScript |
Tags |
reliability, suspicious-comparison |
Description
Reports comparisons against the literal -0: x === -0, x !== -0, x > -0, x ⇐ -0, etc. JavaScript’s equality and relational operators treat 0 and -0 as the same value, so the comparison never distinguishes them.
Rationale
JavaScript follows IEEE 754: positive and negative zero are distinct bit patterns but equal under ===, ==, <, >, ⇐, and >=. Writing x === -0 reads as "test for negative zero", but it fires for positive zero too. The intent is hidden, the bug is silent.
// Bad - same as `x === 0`
if (x === -0) handleNegativeZero();
Remediation
If the intent is to detect negative zero specifically, use Object.is:
// Good
if (Object.is(x, -0)) handleNegativeZero();
If the intent is just zero (either sign), drop the minus sign — x === 0 is clearer.