JavaScript Number.NaN

The syntax to access the NaN property is:

Number.NaN

NaN is accessed using the Number class name.


Example: Using Number.NaN

// verify value is a number
function verify(x) {
  if (isNaN(x)) {
    return Number.NaN;
  }
  return x; // only return if number is not NaN
}

console.log(verify(5)); // 5
console.log(verify(5.45)); // 5.45
console.log(verify(Infinity)); // Infinity

console.log(verify("five")); // NaN
console.log(verify(undefined)); // NaN
console.log(verify([1, 2, 3, 4])); // NaN

Output

5
5.45
Infinity
NaN
NaN
NaN

Recommended Reading:

Did you find this article helpful?

Your builder path starts here. Builders don't just know how to code, they create solutions that matter.

Escape tutorial hell and ship real projects.

Try Programiz PRO
  • Real-World Projects
  • On-Demand Learning
  • AI Mentor
  • Builder Community