javascriptnangoogle-closure-library

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


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.

The underlying code is:

goog.math.isFiniteNumber = function(num) {
  return isFinite(num) && !isNaN(num);
};

So, first it checks whether the number is finite using the native isFinite function, and then does an additional check to make sure the number isn't NaN using isNaN.

However, isFinite already returns false in case the argument is NaN. So, what advantages does the check for isNaN provide?


Solution

  • If isFinite worked the way isFiniteNumber did, then there would be no reason to write isFiniteNumber. There's probably some browser out there somewhere that treats NaN as finite.