JavaScript isnan() Function with Examples

JavaScript isnan() Function with Examples | An element’s status as NaN is determined by the isNaN() method. Use Number.isNaN instead of the isNaN function since coercion inside of it can be unexpected (). NaN stands for “Not-a-Number” in JavaScript. When a value is NaN, the isNaN() function evaluates to true. Prior to being tested, the value is converted to a number via the isNaN() function.

When a value is supplied with a type of Number, the Number.isNaN() approach checks to see if it is NaN. It is an improved iteration of the initial global isNaN(). The syntax for JavaScript isnan() function is given below:

Syntax for JavaScript isnan() function:
isNaN(value)

The equality operators (== and ===) cannot be used to check a number to NaN to discover if it is NaN or otherwise, unlike all the other possible values in JavaScript, since both NaN == NaN and NaN === NaN result to false. The method isNaN() offers a handy equal check for NaN.

JavaScript isnan() Function Examples

Let us have a look at some examples of the JavaScript isnan() function to understand the concept even better.

console.log(isNaN(NaN))
console.log(isNaN({}))
console.log(isNaN(undefined))
console.log(isNaN(false))
console.log(isNaN(true))
console.log(isNaN(null))

Output:-

true
true
true
false
false
false

console.log(isNaN(22))
console.log(isNaN(14.89))
console.log(isNaN('14.89'))
console.log(isNaN("9843"))
console.log(isNaN("XYZW12"))

Output:-

false
false
false
false
true

‘14.89’ is converted to 14.89 which is a number therefore isNaN() function returns false for it. Similarly, “9843” is also converted to 9843 which is a number therefore isNaN() function returns false. But “XYZW12” is converted using parseInt(“XYZW12”) and gives NaN, hence isNaN() function give result as true.

Whenever arithmetic operations produce undefinable or ill-defined results, NaN values are issued. These numbers don’t always indicate overload situations. A NaN also comes from attempts to force non-numeric data into numerical values when there isn’t a primitive numeric value for those non-numeric variables. For instance, dividing zero by zero yields a NaN, while doing so with other numbers do not.

The behavior of the isNaN feature with regard to non-numeric parameters has been unclear since its very first iterations. The value is initially forced to be a Number whenever the input to the isNaN method is not of the type Number. The value that results is then checked to see if it’s NaN. The “false” retrieved value may therefore be unexpected for non-numbers that, once converted to a numeric type, yield a legitimate non-NaN integer value (particularly the vacant string which, when converted, yields numeric values zero or one). The empty string, for instance, is unquestionable “not a number.”

The difference between Number and isnan() is that If a value is Not a Number (NaN), the isnan() isNaN() method evaluates to true. Number.isNaN()  evaluates to true if a number is Not-a-Number. In other terms, the function isNaN() tests the value after converting it to a number. This brings us to the end of the article, where we learned the basics of the isNaN function in JavaScript.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.