Why check for !isNaN() after isFinite()?

后端 未结 5 1058
你的背包
你的背包 2021-02-01 17:04

I came across the goog.math.isFiniteNumber function in the Google Closure Library. What it does is checking whether a given number is both finite and not NaN<

5条回答
  •  你的背包
    2021-02-01 17:50

    The only difference is this:

    !isNan(1/0) // --> true
    isFinite(1/0) // --> false
    

    isNaN checks whether the argument is a number or not. The Infinities (+/-) are also numerical, thus they pass the isNaN check, but don't pass the isFinite check.

    ** Note that any string which can be parsed as a number ("2", "3.14") will cause isNaN to return false.

    Hope this helps.

    PS: The answer given by user1170379 was very nearly perfect.

提交回复
热议问题