Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

typescript

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

The following does not work:

someObject.someValue == NaN someObject.someValue === NaN 
like image 902
ajaysinghdav10d Avatar asked Mar 07 '17 17:03

ajaysinghdav10d


People also ask

How do I check if a value is NaN in TypeScript?

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 .

Is NaN a number in TypeScript?

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.

How do I know if a number is NaN?

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.

How can you reliably test if a number is NaN?

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.


1 Answers

Same as JavaScript, isNaN.

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

Or the more modern Number.isNaN

if (Number.isNaN(someObject.someValue)) ... 
like image 73
zeh Avatar answered Sep 27 '22 20:09

zeh