Dmitriy
Dmitriy

Reputation: 3765

Why 0 === -0 is true, but 1/0 === 1/-0 is false?

var a = 0;
var b = -a;

When I post the following code to console I got true:

console.log(a === b); // true

But when I do some calculation with it I got false:

console.log(1/a === 1/b); // false

Why is it so?

Upvotes: 7

Views: 144

Answers (1)

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67207

That is because Infinity == -Infinity is false, as per abstract equality comparison algorithm.

1/0 will yield Infinity at the same time 1/-0 Yields -Infinity. So both are not are not equal and thus returning false.

Upvotes: 7

Related Questions