How do I check if a number evaluates to infinity?

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

    I like to use Lodash for a variety of defensive coding reasons as well as readability. ES6 Number.isFinite is great and does not have issues with non-numeric values, but if ES6 isn't possible, you already have lodash, or want briefer code: _.isFinite

    _.isFinite(Infinity); // false
    _.isFinite(NaN); // false
    _.isFinite(-Infinity); // false
    
    _.isFinite(null); // false
    _.isFinite(3); // true
    _.isFinite('3'); // true
    
    0 讨论(0)
提交回复
热议问题