85

In TypeScript, how can we check if some value is NaN?

The following does not work:

someObject.someValue == NaN
someObject.someValue === NaN
ajaysinghdav10d
  • 1,771
  • 3
  • 23
  • 33
  • 1
    I seconded this question because it is not (or is no longer) the same as asking about isNaN in Javascript, because Typescript disallows testing anything but a number now -- per the last comment below. – GG2 Jan 22 '23 at 23:38

1 Answers1

150

Same as JavaScript, isNaN.

if (isNaN(someObject.someValue)) ...

Or the more modern Number.isNaN.

if (Number.isNaN(someObject.someValue)) ...

The difference between the two is that:

  • isNaN() will coerce any non-number values (values with typeof value !== 'number') into a number, and then perform the comparison; it's checking to anything that becomes NaN when attempted to convert to a number
  • Number.isNaN() always returns false when it receives a non-number; it's actually checking for what is already NaN

In other words, with these values, you get these results:

value isNaN() Number.isNaN() typeof value === 'number'
123 false false true
NaN true true true
true/false false false false
'a string' true false false
new Date() false false false
[] false false false
{} true false false
zeh
  • 10,130
  • 3
  • 38
  • 56
  • 5
    Anything that is not `NaN` returns `false`. Strings, `null`, `undefined`, booleans - they'll all return `false` from `Number.isNaN()` or plain `isNaN()`. The function does not test if something "isn't a number", but rather if it "is NaN" which is a specific invalid number value, in practice. To test if something is not a number, do `typeof something !== "number" || isNaN(something)`. – zeh Feb 18 '19 at 14:42
  • 4
    In case anyone else comes upon this, `window.isNaN()` will coerce a value into a number and THEN check if it is `NaN`. So `undefined` and `"hello"` will return `true`, while `undefined` and `""` return `false`. This is why `Number.isNaN` was added, because this does check explicitly for `NaN` without the initial coercion. – Brook Jordan May 08 '20 at 09:07
  • Worth keeping in mind: `"123.123"` will return false, but `"123,123"` will return true. – shoopi Apr 07 '21 at 16:37
  • 2
    @zeh As per Typescript (es5+) `isNaN()` only takes a `number` type as its first argument. – S.D. Jul 14 '21 at 09:17
  • 1
    Hmm, this table is not 100% correct. When testing inside a browser `isNaN([])` returns `false`, which does make sense since `[]` is coerced into `0`. Or is this a recent change? – Clarity Mar 21 '23 at 12:41
  • 1
    @Clarity You're right. I just checked the table and the description from references and it's coerced first. The idea that "non-numbers will always return `true`" is incorrect. I'm not sure if this is a recent change; it seems unlikely. I think it's more likely the original edit (done by a different author) was factually wrong. I've re-done all checks and updated the table and the description to be correct (the date and boolean values were also incorrect). – zeh Mar 22 '23 at 20:41
  • 1
    @zeh thanks for the update! I was working on a [similar table](https://claritydev.net/blog/what-is-the-type-of-nan#testing-for-nan-values-numberisnan-vs-isnan) but the results are not hardcoded and are the actual output of the statements using the value and it wasn't matching the results here, which got me wondering. – Clarity Mar 23 '23 at 06:25