How can you reliably test if a value is equal to NaN?Ask a question
NaN property represents a value that is “not a number”. This special value results from an operation that could not be performed either because one of the operands was non-numeric (e.g.,
"abc" / 4), or because the result of the operation is non-numeric.
While this seems straightforward enough, there are a couple of somewhat surprising characteristics of
NaN that can result in hair-pulling bugs if one is not aware of them.
For one thing, although
NaN means “not a number”, its type is, believe it or not, Number:
console.log(typeof NaN === "number"); // logs "true"
NaN compared to anything – even itself! – is false:
console.log(NaN === NaN); // logs "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. Also, ES6 offers a new
Number.isNaN() function, which is a different and more reliable than the old global isNaN() function.
- 2Where can I get some?
- 0Return value optimization
- 0Why is the sizeof of empty struct equal 1 ?
- 0Meanings of zero-, value- and default-initialization
- 1C++: Can I call class constructor from another constructor ?
- 1Where i can get sanskrit translation of class 8 NCERT book in hindi ?
- 0Additional key/value pair does not appear when added to an Object
- 0How can I spread out my flex items to more than the width of a container?
- 1How can I check if a string contains an AlphaNumeric character?
- 0How can block container establish both block and inline formatting contexts at the same time?
- 0Get computed value of CSS variable that uses an expression like calc