How to Check if a Value Is NaN in JavaScript
-
Use the Built-In
isNaN()
Function to Check if a Value IsNaN
in JavaScript -
Combine
isNaN()
WithparseFloat()
to Handle Empty Strings -
Check if a Value Is
NaN
by Using a Comparison Operator in JavaScript
NaN
values in JavaScript can perplex newcomers to the language, especially since they are often confused with undefined
or Null
values. NaN
is an acronym for Not-a-Number
, making their purpose quite obvious.
JavaScript can make it difficult to distinguish between different undefined values. Boolean operations also have a couple of gotchas you must be aware of when dealing with NaN
values.
There are situations where one has to check and see if a certain value is of type NaN
. As such, here are some methods you can employ to check if a value is NaN
.
Use the Built-In isNaN()
Function to Check if a Value Is NaN
in JavaScript
One of the most useful ways to perform this check is to use the standard library method isNaN()
. If the value you give it is NaN
, then the return of this function will be true
. Here is an example of how you can use this in your code:
let myvar_1 = 'dog';
let myvar_2 = '2';
console.log(isNaN(myvar_1));
console.log(isNaN(myvar_2));
Output:
true
false
Combine isNaN()
With parseFloat()
to Handle Empty Strings
As mentioned above, there are some gotchas you have to watch out for. One of them involves empty strings, which will return false
when using the isNaN()
function. Here is an example:
let myvar_1 = '';
console.log(isNaN(myvar_1));
Output:
false
Naturally, that is probably not the result you want in this case. To properly handle these cases, you can add the parseFloat()
method inside the isNaN()
function as follows:
let myvar_1 = '';
console.log(isNaN(parseFloat(myvar_1)));
Output:
true
Check if a Value Is NaN
by Using a Comparison Operator in JavaScript
The following method is even faster than the ones detailed above, and it also requires less code. On the other hand, it is a bit more confusing, not to mention that it may prove to be hard to maintain and document properly.
In short, JavaScript NaN
values are the only ones that are not equal to themselves. Because of this, a simple comparison can easily tell you if the value you are interested in is NaN
or not. Here is how it works:
let myvar_1 = NaN;
let myvar_2 = 'dog';
let myvar_3 = '2';
console.log(myvar_1 !== myvar_1);
console.log(myvar_2 !== myvar_2);
console.log(myvar_3 !== myvar_3);
Output:
true
false
false
This method works differently than the isNaN()
function. In the previous example, strings that can be parsed as numbers are considered numbers. If you are using the comparison operator, then the same strings will not be parsed as numbers. This specific example only returns true
if the value is clearly defined as NaN
.
Both methods have their merits, but you should know that they operate differently, so we should avoid mixing them in the same codebase.
Related Article - JavaScript Variable
- How to Access the Session Variable in JavaScript
- How to Check Undefined and Null Variable in JavaScript
- How to Mask Variable Value in JavaScript
- Why Global Variables Give Undefined Values in JavaScript
- How to Declare Multiple Variables in a Single Line in JavaScript
- How to Declare Multiple Variables in JavaScript