Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

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?

like image 455
Dmitriy Avatar asked Mar 20 '16 14:03

Dmitriy


1 Answers

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.

like image 76
Rajaprabhu Aravindasamy Avatar answered Oct 20 '22 01:10

Rajaprabhu Aravindasamy