NaN or Not A Number can cause issues when you’re trying to pay your gas bill.

In JavaScript, the number type can be either an integer or a float, as such…

Now, stands for “Not A Number”, and it means, well, whatever you’re looking at is not a number, that being said we can’t do something like…

Because even though our variable is not a number, it is still a valid data type and JavaScript tells us what that type is. However, if we did something like this…

There’s our ! (A quick aside, our outputs because is still a number type, according to JavaScript, however it does not equal any value, even itself. yields ) This example kind of highlights the main benefit of , which is to find where we might have a failed operation on a number.

To manually test if an operation will yield , we can use the global function

We could also use

The difference between and is that will not convert its argument into a number…

In these examples, converts into a number, which it is of course not, and tells us that it is that is . Conversely, does not perform this conversion and tells us that it is that does not equal .

Situations where you may run into NaN

1. Parsing Integers

When parsing fails in JavaScript you’ll get a

2. Using undefined as an operand

If we try to use undefined in arithmetical operations…

3. Using NaN as an operand

The same idea as above…

4. When there is an indeterminate form

When arithmetical operations can’t quite give us a straight answer…

5. Invalid arguments in a math function

More things that math doesn’t like…

Well there you have it, a quick run through of or “Not a Number” and some situations where you may run into it in the wilds. Just remember that can be your friend and help you find some errors in operations in your code.

I hope this helps, and thanks for reading!

Computer Support Tech turned Software Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store