Search code examples
javascriptinfinity

How do I check if a number evaluates to infinity?


I have a series of Javascript calculations that (only under IE) show Infinity depending on user choices.

How does one stop the word Infinity appearing and for example, show 0.0 instead?


Solution

  • 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))
    {
        // ...
    }