Given a value, which two options can a developer use to detect if the value is NaN?
Comprehensive and Detailed Explanation From Exact Extract JavaScript knowledge:
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)
________________________________________
Annelle
1 day ago