Skip to content

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.

function checkValue(value: number) {
if (value === -0) {
return "negative zero";
}
return "other";
}
// Also incorrect with other operators
if (value == -0) {
}
if (value !== -0) {
}
if (value < -0) {
}
if (value >= -0) {
}

This rule is not configurable.

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).

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.