Question

Consider the following

var l = console.log.bind(console);
l(-0); // 0
l(0); // 0
l(0 === -0); // true
l(0 == -0); // true
l(1 / 0); // Infinity
l(1 / -0); // -Infinity
  • Why is negative zero equal to zero ?
  • Given it's equal why does it behave differently ?

Bonus question:

  • Is the 0/-0 combination the only combination where equal objects behave differently?

I know NaN/NaN is a combination where non-equal objects behave the same.

Was it helpful?

Solution

Why is negative zero equal to zero ?

Because IEEE 754 demands it.

Is the 0/-0 combination the only combination where equal objects behave differently?

I believe so. In Javascript, only Numbers have a special === algorithm, and 0, -0, NaN are the only special cases there (ECMA-262 §11.9.6).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top