In TypeScript, how can we check if some value is NaN
?
The following does not work:
someObject.someValue == NaN someObject.someValue === NaN
To check if a value is NaN , call the Number. isNaN() method, passing it the value as a parameter. The Number. isNaN method returns true if the passed in value is NaN and has a type of number , otherwise it returns false .
NaN in Typescript stands for Not a Number. It is the result of numerical operations, where result is not a number . It is the property of the global object. You can refer it either as NaN or Number.
isNaN() Method: To determine whether a number is NaN, we can use the isNaN() function. It is a boolean function that returns true if a number is NaN otherwise returns false.
A semi-reliable way to test whether a number is equal to NaN is with the built-in function isNaN(), but even using isNaN() is an imperfect solution. A better solution would either be to use value !== value, which would only produce true if the value is equal to NaN.
Same as JavaScript, isNaN
.
if (isNaN(someObject.someValue)) ...
Or the more modern Number.isNaN
if (Number.isNaN(someObject.someValue)) ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With