What is the best way to check a given variable is NaN or not?
(only using pure Javascript without any libraries such as Underscore)
What is the best way to check a given variable is NaN or not?
(only using pure Javascript without any libraries such as Underscore)
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
You can use isNaN(x)
which returns a boolean value (true if x is NaN, false otherwise).