What is NaN, what is its type, and how to check for it?

👨‍💻 Frontend Developer 🟠 May come up 🎚️ Easy
#JavaScript #Data Types #JS Basics

Quick Answer

NaN (Not-a-Number) is a special numeric value in JavaScript that represents the result of an invalid or undefined mathematical operation. Despite its name, its data type is number. The most reliable way to check for NaN is the Number.isNaN() method, because NaN is not equal to anything, including itself (NaN === NaN returns false).


What is NaN

NaN is a global property representing the value “not a number”. It is one of the special values of the number type in the IEEE 754 standard, used to represent results that cannot be expressed as real numbers.

NaN Data Type

Despite its name, NaN is technically a number.

console.log(typeof NaN); // "number"

Features:

  • NaN belongs to the number type.
  • ✅ This allows numeric operations to return a number even if the result is not a real number, avoiding runtime errors.

How to get NaN

NaN is returned when a mathematical operation cannot produce a meaningful numeric value.

1. Invalid Mathematical Operations

// Dividing zero by zero
console.log(0 / 0); // NaN
 
// Taking the square root of a negative number
console.log(Math.sqrt(-1)); // NaN
 
// Multiplying infinity by zero
console.log(Infinity * 0); // NaN

2. Converting Non-Numeric Strings

When JavaScript tries to convert a string that does not contain a number into a number, the result will be NaN.

// Explicit conversion
console.log(parseInt('hello')); // NaN
console.log(Number('abc')); // NaN
 
// Implicit conversion in mathematical operations
console.log('string' - 10); // NaN
console.log('10a' * 5); // NaN

Features:

  • NaN is “contagious”: any operation with NaN will return NaN.
  • console.log(1 + NaN); // NaN
  • console.log('text' + NaN); // "textNaN" (string concatenation occurs here)

Checking for NaN

Checking for NaN has its peculiarities, as NaN is the only value in JavaScript that is not equal to itself.

console.log(NaN === NaN); // false
console.log(NaN == NaN); // false

This is the most reliable and preferred method. It checks if the passed value is NaN and does not try to convert it.

console.log(Number.isNaN(NaN)); // true
console.log(Number.isNaN(123)); // false
console.log(Number.isNaN('abc')); // false (does not convert the string to a number)
console.log(Number.isNaN(undefined)); // false

Features:

  • ✅ Does not perform type coercion.
  • ✅ Accurately identifies only NaN.
  • ✅ Is part of the ES6 standard.

2. Global function isNaN()

This method is less reliable because it first tries to convert the argument to a number.

console.log(isNaN(NaN)); // true
console.log(isNaN('abc')); // true, because Number('abc') -> NaN
console.log(isNaN(undefined)); // true, because Number(undefined) -> NaN
console.log(isNaN({})); // true, because Number({}) -> NaN

Features:

  • ❌ Can produce false positives.
  • ❌ Is considered outdated and unreliable for accurate checking.

3. Checking for inequality to itself

This is a “hacky” but working method based on the unique property of NaN.

function isValueNaN(value) {
  return value !== value;
}
 
console.log(isValueNaN(NaN)); // true
console.log(isValueNaN(123)); // false
console.log(isValueNaN('abc')); // false

Features:

  • ✅ Works reliably and quickly.
  • ✅ Does not require special functions.
  • ❌ Less readable and obvious than Number.isNaN().

Practice Problems

Problem 1

// What will this code output to the console?
console.log(typeof NaN);
console.log(NaN === NaN);
console.log(Number.isNaN('hello'));
console.log(isNaN('hello'));
Answer **Explanation:** 1. The type of `NaN` is `number`. 2. `NaN` is not equal to itself. 3. `Number.isNaN()` does not convert types, so a string is not `NaN`. 4. `isNaN()` converts the string 'hello' to `NaN` and returns `true`.

Problem 2

// Write a function that takes an array and returns
// only numeric values, excluding NaN.
 
function filterNumbers(arr) {
  // Your code
}
 
const data = [1, 'a', 2, NaN, 3, undefined, 4, null, '5'];
console.log(filterNumbers(data)); // [1, 2, 3, 4]
Answer
function filterNumbers(arr) {
  return arr.filter(item => typeof item === 'number' && !Number.isNaN(item));
}

Explanation: We filter the array, keeping only elements with the type number, and additionally ensure that it is not NaN using Number.isNaN().