How do I check if a number evaluates to infinity?
Solution 1:
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))
{
// ...
}
Solution 2:
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