
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Test If a Value Is Equal to NaN in JavaScript
This article discusses about how do you test if a value is equal to NaN in JavaScript. In JavaScript, NaN is a method from Number class. NaN implies Not-a-Number. It is of Boolean type. It returns true when the value is "Not-a-number".
The NaN method is used in the situations. For example, when a function tries to parse a number and it fails or when a math function fails, the method NaN is used. The syntax for isNaN() method is shown below.
isNaN(); or Number.isNaN();
There is a difference between isNaN() and Number.isNaN(). If the value is currently NaN or will be NaN after being converted to a number, isNaN() will return true. To put it another way, isNaN() will return false if it receives a value that can be converted to a number. But the function Number.isNaN() returns true, only if the value is currently NaN.
Let us see a few examples for these ?
Example 1
The following example below will check whether a value is equal to NaN or not by using isNaN() method.
<html> <body> <p id="height"></p> <script> document.write(isNaN('hello world') + "<br>"); document.write(Number.isNaN('hello world')); </script> </body> </html>
On executing the above code, the following output is generated.
Example 2
The following is another example program to check whether a value is equal to NaN or not by using isNaN() method.
<!DOCTYPE HTML> <html> <head> <title>To check whether a number is NAN or not</title> </head> <body style = "text-align:center;"> <h3>A simple program to check whether a number is NAN or not.</h3> <p id="text1"></p> <script type="text/javascript"> function check(value){ if(isNaN(value)){ return 'is NaN'; }else{ return 'is not a NaN'; } } var a = check('100'); var b = check('Tutorials Point'); var c = check('100.234'); var d = check('2/3'); var e = check('15/04/2022'); document.getElementById("text1").innerHTML = "100 "+a+'<br />'+"TutorialsPoint "+b+'<br />'+"100.234 "+c+'<br />'+"2/3 "+d+'<br />'+"15/04/2022 "+e+'<br />'; </script> </body> </html>
On executing the above code, the following output is generated.
Example 3
The below example uses the Number.isNaN() method. The Number.isNaN() returns true if the given value is NaN and its type is Number; or else it returns false. The isNaN() method converts the value to a number before testing it. It is not possible in the case of Number.isNaN() method.
<!DOCTYPE HTML> <html> <head> <title>To check whether a number is NAN or not</title> </head> <body style = "text-align:center;"> <h3>A simple program to check whether a number is NAN or not by using Number.isNaN method.</h3> <p id="text1"></p> <script type="text/javascript"> function check(value){ if(Number.isNaN(value)){ return 'is NaN'; }else{ return 'is not a NaN'; } } var a = check(100); var b = check('Tutorials Point'); var c = check(0/0); var d = check(Math.sqrt(-100)); var e = check('15/04/2022'); document.getElementById("text1").innerHTML = "100 "+a+'<br />'+"TutorialsPoint "+b+'<br />'+"0/0 "+c+'<br />'+"Square root ?-100 "+d+'<br />'+"15/04/2022 "+e+'<br />'; </script> </body> </html>
On executing the above code, the following output is generated.