JavaScript

JavaScript isNaN() Function | Explained

In JavaScript, you cannot completely rely on the equality operators to determine if a value is a number or not. For this reason, ES6 embedded a method named “isNaN()” to check whether a value is not a “NaN(Not a Number) or not. If the specified value is a number, this method will return “false“; otherwise, its return case is set to “true” for a NaN value.

This write-up will explain the usage of the JavaScript isNaN() function. So, let’s start!

JavaScript isNaN() Function

The term “isNan” comprises two words “is” and “NaN”, where “NaN” is an acronym for “Not a Number” and adding the helping verb “is” in front of NaN turns it into a question that states whether a value is a “Not a Number”?

The JavaScript “isNaN()” function is also utilized to check if a value is acceptable or not to reassure the client-side security.

Syntax

isNaN(value)

Here, the “isNaN()” function will validate the “value” passed an argument and return a boolean value that can be “true” or “false”.

Now, let’s check out some examples related to the usage of the JavaScript isNaN() function.

Example 1

When a positive decimal number such as “678” is passed as an argument, the “isNaN()” function will return “false”:

console.log(isNaN(678))

Output

Example 2

For a negative decimal number, the return case of the JavaScript “isNaN()” function is set to “false”. For instance, we have added “-6.78” as an argument for the “inNaN()” function:

console.log(isNaN(-6.78))

The execution of the above-given code will print out “false” on the console window:

Example 3

The “undefined” value in JavaScript does not comprise any data that can be converted into a number, so passing it in the JavaScript “isNaN()” Function will result in a “true” value:

console.log(isNaN(undefined))

Output

Example 4

In the below-given example, we have specified the string “NaN” as an argument in the “isNaN()” function:

console.log(isNaN('NaN'))

The “NaN” string cannot be converted into a number because there is a non-numeric value within the quotes, that’s why the JavaScript “isNaN()” function will return “true”:

Example 5

We will pass the string “789” to the “isNaN()” function:

console.log(isNaN('789'))

Here, the “789” string will be converted into a number that is valid, then the JavaScript “isNaN()” function will return “false” after marking it as a numeric type:

Example 6

The added “linuxhint” string in the following “isNaN()” function cannot be converted into a number because it has non-numeric value; as a result of it, the execution of the “isNaN()” function will set its return case as “true”:

console.log(isNaN('linuxhint'))

Output

Example 7

In this example, today’s date “2022/03/23” is specified as an argument of the “isNaN()” function:

console.log(isNaN('2022/03/23'))

Hence the added value is the string representation of the data which cannot be converted into a number, so the JavaScript “isNaN()” will output “true”:

JavaScript beginners often think that the “isNaN()” function and the “Number.isNaN()” method works in the same way, but that’s not the case. There exists a significant difference between both of them.

Have a look at the following section to clear the confusion about the working of the “isNaN()” function and the “Number.isNaN()” method in JavaScript.

Difference between JavaScript isNaN() function and Number.isNaN() method

In JavaScript, “isNaN()” is a global function that converts the specified argument to a “number” and then evaluates it, whereas the “Number.isNaN()” is a method of the JavaScript “Number” base class that checks if the passed argument is “Not a Number”, without converting it to the “number” type.

For instance, in the following code, both arguments are of “string” type which signifies that they are not numbers:

console.log(isNaN('linuxhint'));
console.log(isNaN('2022/3/23'));

The “isNaN()” will return “true” after validating the specified arguments:

However, the “Number.isNaN()” method will only output “true” of the argument is of “Number” type and its value “NaN”:

Both of the specified conditions does not imply in the below-given statements, so the execution of the “Number.isNaN()” method will return “false”:

console.log(Number.isNaN('linuxhint'));
console.log(Number.isNaN('2022/3/23'));

Output

That was all essential information related to the JavaScript isNaN() function. You can further research it according to your preferences.

Conclusion

The JavaScript “isNaN()” function can be utilized to check whether a value is a “NaN” (Not a Number) or not. It is a global function that converts the specified argument to a number and then evaluates it. If the specified value is a number, then the JavaScript “isNaN()” method will return “false“; otherwise, its return case is set to “true” for a NaN value. This write-up explained the usage of the JavaScript isNaN() function.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.