We already know NaN is special: it is not equal to itself.
Check each:
A. value === Number.NaN
Number.NaN is NaN.
NaN === NaN is always false.
This will never be true; cannot reliably detect NaN.
B. value == NaN
NaN == NaN is also always false.
Again, this never detects NaN.
C. isNaN(value)
Global isNaN converts its argument to a number and then checks if the result is NaN.
This can detect NaN, but it may also return true for non-number values that coerce to NaN, such as isNaN( ' foo ' ).
Regardless, it is a standard way to detect if a value is “NaN-like” in JavaScript.
D. Object.is(value, NaN)
Object.is(NaN, NaN) returns true.
This is a strict way to detect a value that is exactly NaN (no coercion).
Therefore, among the given choices, the two viable ways to detect NaN are:
isNaN(value)
Object.is(value, NaN)
Contribute your Thoughts:
Chosen Answer:
This is a voting comment (?). You can switch to a simple comment. It is better to Upvote an existing comment if you don't have anything to add.
Submit