Kodeclik Blog


What is NaN in Javascript?

We will learn what NaN (Not a Number) means in Javascript. For this purpose, we will write a Javascript program and embed it inside a HTML page like so:
In the above HTML page you see the basic elements of the page like the head element containing the title (“Nan in Javascript”) and a body with an empty script tag. Any HTML markup goes inside the body tags and any Javascript code goes inside the script tags.

NaNs are the result of nonsensical operations

Consider the following code that attempts to multiply a string by an integer.
The output is:
Any similar nonsensical operation will also lead to an NaN.
Another example of an NaN is if we try to find square roots of negative numbers:
The output is:

NaNs used in operations will lead to more NaNs

Consider the following code:
The output is:
If we try to find the square root of a NaN, we will again get an NaN:
with the output:

Division by zero

Let us attempt to divide an integer by zero:
The output is:
So it appears like Javascript has a literal called Infinity. If you attempt to multiply Infinity by, say, zero:
we obtain:
So infinity times zero is a NaN. Infinity divided by infinity is also an NaN:
The output is:

Checking if an object is a NaN

The predicate isNaN() returns true if its input is not a number and false otherwise. Here is an example of how to use it:
The output is:
As expected, 3 is not an NaN and Infinity is also not an NaN (as we saw in the example earlier). Of the three inputs, only “hello” is an NaN.

Assigning NaN values to variables

Sometimes you will have a need to assign NaN explicitly to variables.
The output is:
You can use this capability when you want to keep track of illegal values for variables in your program. For instance if you are requesting a customer for their age in a program (intending to later doing some operations with it) and the customer enters a string or non-numeric for their age, you can assign a NaN to it and then future operations will be able to detect that they have a not-a-number for the age.

NaNs are not equal to NaNs

Because NaN simply denotes “not a number” two variables that have values as NaN are not necessarily equal to each other. Consider the following program:
The output is:
Note that this is true irrespective of whether you use “==” or “===” in your if condition.
In fact, this property of NaNs (i.e., it is not equal to itself) can be used as a check to see if a given input is an NaN (simply check if it is equal to itself). This gives you an alternative to using the isNaN() function we encountered earlier.
If you liked learning about NaNs, checkout our blogpost on computing powers and how NaNs occur in those operations. Another case where NaNs occur is when you are processing user input. See our detailed blogpost on gathering user input.
Interested in learning more Javascript? Learn how to loop through two Javascript arrays in coordination and how to make a Javascript function return multiple values! Also learn about Javascript's 'if not' construct.
Want to learn Javascript with us? Sign up for 1:1 or small group classes.

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.
  • ABOUT

Copyright @ Kodeclik 2023. All rights reserved.