In this article, we will learn what does NaN mean in JavaScript programming with code example.

In JavaScript, NaN stands for “Not a Number.” It’s a special value that represents the result of an undefined or unrepresentable mathematical operation, such as the square root of a negative number, or the result of trying to parse an invalid number from a string.

For example, the following code will return NaN:

parseFloat("not a number"); // NaN
Math.sqrt(-1); // NaN

It’s important to note that NaN is a unique value in JavaScript, meaning that NaN is not equal to any other value, including itself. So the following comparison is not working as it should

if (value === NaN) {
  // This block will not execute if value is NaN

To check if a value is NaN you can use the function isNaN(value)

if (isNaN(value)) {
  // this block will execute if value is NaN

Another way to check NaN values is the Number.isNaN(value) it introduced in ES6 that is more accurate than the global isNaN()

if (Number.isNaN(value)) {
  // this block will execute if value is NaN

It’s important to note that in Javascript NaN is of type number, it’s a number but a special one, that you should take care when working with it.

Hope you liked this article on what does nan mean in javascript, Follow us on Facebook and Instagram.