In JavaScript, why does zero divided by zero return NaN, but any other divided by zero return Infinity?

Bloodyaugust picture Bloodyaugust · Sep 17, 2013 · Viewed 28.9k times · Source

It seems to me that the code

console.log(1 / 0)

should return NaN, but instead it returns Infinity. However this code:

console.log(0 / 0)

does return NaN. Can someone help me to understand the reasoning for this functionality? Not only does it seem to be inconsistent, it also seems to be wrong, in the case of x / 0 where x !== 0

Answer

Oliver Charlesworth picture Oliver Charlesworth · Sep 17, 2013

Because that's how floating-point is defined (more generally than just Javascript). See for example:

Crudely speaking, you could think of 1/0 as the limit of 1/x as x tends to zero (from the right). And 0/0 has no reasonable interpretation at all, hence NaN.