How do I check if a number evaluates to infinity?

Actually n === n + 1 will work for numbers bigger than 51 bit, e.g.

1e16 + 1 === 1e16; // true
1e16 === Infinity; // false

In ES6, The Number.isFinite() method determines whether the passed value is a finite number.

Number.isFinite(Infinity);  // false
Number.isFinite(NaN);       // false
Number.isFinite(-Infinity); // false

Number.isFinite(0);         // true
Number.isFinite(2e64);      // true

A simple n === n+1 or n === n/0 works:

function isInfinite(n) {
  return n === n/0;
}

Be aware that the native isFinite() coerces inputs to numbers. isFinite([]) and isFinite(null) are both true for example.


if (result == Number.POSITIVE_INFINITY || result == Number.NEGATIVE_INFINITY)
{
    // ...
}

You could possibly use the isFinite function instead, depending on how you want to treat NaN. isFinite returns false if your number is POSITIVE_INFINITY, NEGATIVE_INFINITY or NaN.

if (isFinite(result))
{
    // ...
}