How do I check if a number evaluates to infinity?

前端 未结 7 814
孤城傲影
孤城傲影 2020-12-07 19:24

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 a

7条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-07 20:09

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

提交回复
热议问题