Skip to main content

JavaScript Type System

JavaScript NaN

Written by Published

NaN means Not a Number, and it is the result of a failed number operation.

Confusingly, typeof NaN is number.

It is a number value that represents an impossible result.

Example

Example

javascript

const result = Number("hello");

console.log(result);
console.log(typeof result);

The output is NaN then number.

What Produces NaN

Converting text that is not a number.

Arithmetic with a value that cannot be converted.

Mathematically undefined results, such as 0 / 0.

Syntax

Syntax

javascript

Number.isNaN(value);

This is the safe way to check.

NaN Is Not Equal to Itself

This is the strangest rule in JavaScript, and it is deliberate.

It is why you cannot check for NaN with equality.

Example

Example

javascript

const value = NaN;

console.log(value === NaN);
console.log(value === value);

Both are false.

Checking Properly

Number.isNaN is the reliable test.

Example

Example

javascript

console.log(Number.isNaN(NaN));
console.log(Number.isNaN(42));
console.log(Number.isNaN("hello"));

Note the last one is false: a string is not the NaN value.

The Old Global isNaN

The global isNaN converts first, so it answers a different question.

Number.isNaN is almost always the one you want.

Example

Example

javascript

console.log(isNaN("hello"));
console.log(Number.isNaN("hello"));

The first says true because the text cannot become a number.

It Spreads Through Arithmetic

Any calculation involving NaN produces NaN.

Example

Example

javascript

const bad = Number("oops");

console.log(bad + 1);
console.log(bad * 100);

One bad value can quietly ruin a whole calculation.

Guarding Input

Check the conversion before using the result.

Example

Example

javascript

function double(text) {
  const number = Number(text);
  if (Number.isNaN(number)) {
    return "not a number";
  }
  return number * 2;
}

console.log(double("21"));
console.log(double("hello"));

The guard stops NaN escaping into the rest of the program.

Complete Example

Complete Example

html

<!DOCTYPE html>
<html>
<head>
  <title>JavaScript NaN</title>
</head>
<body>

  <h1>NaN</h1>

  <input id="value" value="hello">

  <p id="out"></p>

  <script>
    const entered = document.getElementById("value").value;
    const number = Number(entered);

    document.getElementById("out").innerHTML =
      "Converted: " + number +
      "<br>typeof: " + typeof number +
      "<br>Is it NaN: " + Number.isNaN(number);
  </script>

</body>
</html>

Try It Yourself

Run the above example in the Try It Editor.

Try a real number:

Change the value to 42 and watch the check flip to false.

Important Points

  • NaN is the result of a failed number operation.
  • typeof NaN is number.
  • NaN is not equal to itself.
  • Use Number.isNaN to check for it.
  • It spreads through any calculation it touches.

Conclusion

NaN is JavaScript's way of saying this calculation made no sense.

Its self-inequality is the reason a special check exists.

Guarding conversions stops it spreading silently.