How do I check if a number evaluates to infinity?

前端 未结 7 811
孤城傲影
孤城傲影 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:13

    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.

提交回复
热议问题