Why Does "typeof NaN" return "number" in JavaScript?

The concept of NaN being a number is not exclusive to JavaScript. In fact, it is a widely used numeric data type value in computing which is meant to represent undefined or unrepresentable mathematical values (such as 0/0, etc.):

0/0; // NaN

Math.sqrt(-1); // NaN
Math.log(-1); // NaN

// ...

NaN is actually, rarely used in a program. It is typically returned from a function or operation to indicate a failed operation on numbers. It is used to suggest that the value is not a legal number (where you may be expecting the return value to be a legal number).

This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.