Check NaN with Number.isNaN()
Write a JavaScript function that takes a value and returns true if it is NaN (Not a Number) using Number.isNaN(). Unlike the global isNaN() function, Number.isNaN() does not coerce the value to a number first, making it more reliable.
Number.isNaN() is a static method introduced in ES6 that determines whether the passed value is NaN and its type is Number. It is the reliable version of the global isNaN() function. The global isNaN() first coerces the value to a number (which can produce NaN from non-numeric strings like "hello" → NaN → returns true). Number.isNaN() returns true ONLY for actual NaN values.
NaN is the only JavaScript value that is not equal to itself (NaN === NaN is false). This makes it impossible to check for NaN using equality operators. Number.isNaN() and the global isNaN() are the only ways to detect NaN, but only Number.isNaN() is reliable without coercion side effects.
Time complexity is O(1). Space complexity is O(1). Number.isNaN() is a simple type and value check that returns a boolean. It is the recommended way to check for NaN in modern JavaScript.
Edge cases include actual NaN (returns true), numbers (returns false), strings that look like numbers like "NaN" (returns false because it's a string), undefined (returns false), and non-numeric strings like "hello" (returns false, unlike global isNaN which returns true).
Example Input & Output
Number is not NaN
String is not NaN
NaN is NaN
Undefined is not NaN
String NaN is not NaN
Algorithm Flow

Solution Approach
Best Answers
function solution(val) {
return Number.isNaN(val);
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
