JavaScript Interview Question #14

What is NaN in JavaScript?

JavaScript Fundamentals, Types, Scope & Functions Junior Beginner

Quick Interview Answer

NaN means Not-a-Number. It is a special numeric value representing an invalid or unrepresentable numeric result, such as trying to convert non-numeric text to a number.

Detailed Explanation

NaN means Not-a-Number. It is a special numeric value representing an invalid or unrepresentable numeric result, such as trying to convert non-numeric text to a number.

A notable behavior is that NaN is not equal to itself. Number.isNaN is generally the safest way to test whether a value is specifically NaN because the global isNaN function may first coerce its argument.

Code Example

const result = Number("abc");

console.log(result);              // NaN
console.log(typeof result);       // "number"
console.log(result === NaN);      // false
console.log(Number.isNaN(result)); // true