Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is nan in JavaScript

Understanding NaN in JavaScript

In the world of JavaScript, there are many terminologies and concepts that might initially seem a bit baffling. One such concept is that of 'NaN'. Now, don't panic if you haven't encountered this term before, or if you have and it left you scratching your head.

NaN stands for 'Not a Number'. It's a special type of value that JavaScript returns when it can't interpret something as a number. To put it simply, it's JavaScript's way of saying, "I don't know what you're asking me to calculate, but it's not a number."

Let's start by looking at a simple example.

console.log(Math.sqrt(-1));

This will return NaN because the square root of a negative number is not a real number, and JavaScript doesn't handle imaginary numbers.

When does JavaScript Return NaN?

There are several instances where JavaScript will return NaN. The most common scenario is when you try to perform a mathematical operation with a value that isn't a number.

console.log("Hello, world!" * 2);

In this example, JavaScript is being asked to multiply a string by a number, and it's responding with NaN because it doesn't know how to do that.

isNaN Function: The NaN Detective

JavaScript provides us with a handy function to check if a value is NaN: the isNaN function.

console.log(isNaN("Hello, world!" * 2));  // true

In this example, we're asking JavaScript: "Is the result of multiplying the string 'Hello, world!' by 2 not a number?" And JavaScript is responding with 'true', confirming that it is indeed not a number.

A Quirk in The isNaN Function

As handy as the isNaN function is, it does have a slight quirk. It tries to convert the value you pass into a number first and then checks if the result is NaN. This means that for some values, isNaN might return true, even though the value isn't NaN.

console.log(isNaN("Hello, world!"));  // true
console.log(isNaN("123"));  // false

In the first example, JavaScript tries to convert the string 'Hello, world!' into a number, fails, and then returns true because the result is NaN. In the second example, JavaScript successfully converts the string '123' into a number, so isNaN returns false.

The Number.isNaN Function: The Improved NaN Detective

To avoid this quirk, ES6 introduced a new function: Number.isNaN. This function works similarly to isNaN, but it doesn't try to convert the value into a number first.

console.log(Number.isNaN("Hello, world!"));  // false
console.log(Number.isNaN("123"));  // false
console.log(Number.isNaN(NaN));  // true

In these examples, Number.isNaN correctly returns false for the strings 'Hello, world!' and '123' because they're not NaN, and true for NaN because it is NaN.

Conclusion: Embracing NaN

In your journey as a JavaScript programmer, you will undoubtedly encounter NaN. It may seem like a strange concept at first, but it's just JavaScript's way of telling you that it can't convert something into a number.

So, the next time you see NaN, don't panic. Remember, it's not an error, it's a sign that JavaScript is trying its best to understand what you're asking it to do.

Think of NaN as a misunderstood artist, trying to create a mathematical masterpiece from your code, but sometimes it just ends up with a piece that's abstract to the point of being 'Not a Number'. Embrace the NaN, decode its meaning, and keep coding!