# no-compare-neg-zero

Disallow comparison against -0 which yields unexpected behavior.

Comparison against -0 causes unwanted behavior because it passes for both -0 and +0. That is, x == -0 and x == +0 both pass under the same circumstances. If a user wishes to compare against -0 they should use Object.is(x, -0).

# Incorrect Code Examples

if (x === -0) {
       // ^^ this comparison works for both -0 and +0
}

# Correct code examples

if (x === 0) {
    /* */
}
if (Object.is(x, -0)) {
    /* */
}
More incorrect examples
x == -0
x != -0
x === -0
-0 === -0
-0 == x
-0 >= 1
x < -0
x !== -0
More correct examples
x === 0
0 === 0
Object.is(x, -0)

Source (opens new window)

Last Updated: 11/18/2020, 9:36:33 PM