Code Logo

Check NaN with Number.isNaN()

Published at25 Jul 2026
JavaScript Functions Easy 0 views
Like0

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

Example 1
Input
42
Output
false
Explanation

Number is not NaN

Example 2
Input
"hello"
Output
false
Explanation

String is not NaN

Example 3
Input
NaN
Output
true
Explanation

NaN is NaN

Example 4
Input
undefined
Output
false
Explanation

Undefined is not NaN

Example 5
Input
"NaN"
Output
false
Explanation

String NaN is not NaN

Algorithm Flow

Recommendation Algorithm Flow for Check NaN with Number.isNaN()
Recommendation Algorithm Flow for Check NaN with Number.isNaN()

Solution Approach

function solution(val) {
  return Number.isNaN(val);
}

Best Answers

javascript - Approach 1
function solution(val) {
  return Number.isNaN(val);
}