negativeZeroComparisons
Reports comparisons with -0 that may not behave as expected.
✅ This rule is included in the ts logical
preset.
In JavaScript, -0
and +0
are distinct values, but standard comparison operators (===
, ==
, !==
, !=
, <
, <=
, >
, >=
) treat them as equal.
This can lead to unexpected behavior when you need to distinguish between these values.
The Object.is()
method correctly distinguishes between -0
and +0
, making it the reliable choice when checking for negative zero.
Examples
Section titled “Examples”function checkValue(value: number) { if (value === -0) { return "negative zero"; } return "other";}
// Also incorrect with other operatorsif (value == -0) {}if (value !== -0) {}if (value < -0) {}if (value >= -0) {}
function checkValue(value: number) { if (Object.is(value, -0)) { return "negative zero"; } return "other";}
// Regular zero comparisons are fineif (value === 0) {}
// Negative one and other numbers are fineif (value === -1) {}
// Using -0 in non-comparison contexts is fineconst negativeZero = -0;const result = -0 + 1;
Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If you never need to distinguish between -0
and +0
in your codebase, you might choose to disable this rule.
However, keeping it enabled helps prevent subtle bugs where the distinction matters, such as when working with mathematical calculations that produce negative zero (e.g., -1 / Infinity
results in -0
).
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- Biome:
noCompareNegZero
- Deno:
no-compare-neg-zero
- ESLint:
no-compare-neg-zero
- Oxlint:
eslint/no-compare-neg-zero
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.