0

What is the best way to check a given variable is NaN or not?

(only using pure Javascript without any libraries such as Underscore)

siva636
  • 15,428
  • 23
  • 93
  • 131
  • 3
    [**isNaN**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN) must be considered as the best way. – davidkonrad Dec 07 '15 at 04:50

2 Answers2

5

JavaScript has function isNaN for that purpose. As described in w3schools:

The isNaN() function determines whether a value is an illegal number (Not-a-Number).

This function returns true if the value is NaN, and false if not.

For example:

isNaN(123) // returns false;
isNaN("Hello")  //returns true
dotnetom
  • 23,993
  • 9
  • 50
  • 53
2

You can use isNaN(x) which returns a boolean value (true if x is NaN, false otherwise).