How to check if value is NaN in Typescript? [duplicate]

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 the tested value into a number before checking (values with typeof value !== 'number' will return true), while Number.isNaN() won’t.

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 true false false
'a string' true false false
new Date() true false false
[] true false false
{} true false false
true false false

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)