Your product could be here, in front of thousands of JavaScript developers. Sponsor this spot →
JavaScript Quiz

Why is typeof NaN "number" in JavaScript?

Can you guess what will be the output of the code below?

JavaScript
console.log(typeof NaN);
Show answer and explanation

typeof NaN actually returns "number", but why is that?

You can actually thank the IEEE Standard for Floating-Point Arithmetic (IEEE 754), which is the specification most programming languages base their implementation of floating point numbers on.

Imagine it this way:

Let’s use TypeScript here as an example to bring statical typing to JavaScript and imagine the following scenario:

const result: number = parseFloat(inputArgument);

The code is pretty straight-forward. We want a number, and we have to parse an input argument for that.

But what if parseFloat actually cannot parse a float?

What if we forgot to validate the input and are now passing 'Does this work?' to parseFloat?

And this is where NaN comes into the game.

NaN simply states: “This is not a number”, but in a type-safe way, so you always get a result of type number.

Take the full quiz Next question →

More Numbers questions

  1. Why does 018 - 015 equal 5 in JavaScript?
  2. Why is 0.1 + 0.2 == 0.3 false in JavaScript?
  3. Why is Math.max() < Math.min() true?
  4. What is -7 % 3 in JavaScript?
  5. Why does indexOf not find NaN but includes does?

Related topics