# Javascript: Why is 0 equal to -0?

## Question:

I want to check if a value is negative or not (I am doing it with `Math.sign` ).

``````Math.sign(30) // 1
Math.sign(-10) // -1
Math.sign(0) // 0
Math.sign(-0) // -0
``````

But if the value is `0` or `-0` I cannot know exactly if it is negative or positive, because:

``````Math.sign(30) === 1 -> true
Math.sign(-10) === -1 -> true
Math.sign(0) === -0 -> true // ¿Por qué? ¿No debería ser false?
``````

According to the documentation:

• Similar to `same-value` equality, but `+0` and `-0` are considered equal.

• In the ES5 specification, the `==` comparison is described in Section 11.9.3, The Abstract Equality Algorithm . The `===` comparison in 11.9.6, The Strict Equality Algorithm . (Look it up and read it, they are short and easy to read. Note: read the strict equality algorithm first.) ES5 also describes, in Section 9.12, The SameValue Algorithm for internal use by the JS engine.

• ( Translated )
Comparison `x === y` , where `x` and `y` are values, returns `true` or `false` . This comparison is made as follows:
1. If `Type (x)` is different from `Type (y)` , it returns `false` .
2. If `Type (x)` is `Undefined` , it returns `true` .
3. If `Type (x)` is `Null` , it returns `true` .
4. If `Type (x)` is `Number` , then:
— If `x` is `NaN` , it returns `false` .
— If `y` is `NaN` , it returns `false` .
— If `x` is the same numeric value as `y` , it returns `true` .
If `x` is +0 and `y` is −0, return `true` .
If `x` is −0 and `y` is +0, return `true` .
— Otherwise it returns `false` .
5. If `Type (x)` is `String` , it returns `true` if `x` and `y` are exactly the same sequence of characters (the same length and the same characters in the corresponding positions); otherwise it returns false.
6. If `Type (x)` is `Bool` , return `true` if `x` and `y` are both true or both false; otherwise, it returns `false` .
7. Returns `true` if `x` and `y` refer to the same object. Otherwise, it returns `false` .

## How to differentiate `0` from `-0` ?

You could use `number.toLocaleString()`

The `toLocaleString()` method returns a string with a language-sensitive representation of this number.

### Example:

``````let a = (0).toLocaleString('en'),
b = (-0).toLocaleString('en');

console.log(`\${a} === \${b} =>`, a === b);``````
Scroll to Top