What is a NaN property of a Number object in JavaScript?


In JavaScript, the NaN property is a special value that represents "Not a Number". It is a property of the Number object and can be accessed using Number.NaN.

The NaN property is usually produced as a result of an operation that cannot produce a meaningful result. For example, dividing 0 by 0 or trying to parse an invalid number will both produce NaN.

Here are a few examples of operations that will produce NaN −

Math.sqrt(-1);   // NaN
0/0;             // NaN
parseInt("foo"); // NaN

It is important to note that NaN is not equal to any value, including itself. So, if you want to check if a value is NaN, you cannot use the == or === operators. Instead, you should use the isNaN() function, which is designed specifically for this purpose.

Here's an example of how to use isNaN() −

if (isNaN(someValue)) {
   console.log("someValue is Not a Number");
}

Syntax

Following is the syntax to represent a not a umber −

NaN
Number.NaN

We can call NaN from the Number objects, so even NaN represents Not a Number but a Property of a Number object.

Sometimes it is strictly required to pass a number for an operation, in that case, we can throw a NaN error to show the users that they can enter only the Number value.

Example

You can try to run the following example to learn how to use NaN −

<html>
<head>
   <script>
      function showValue() {
         var dayOfMonth = 50;
         if (dayOfMonth < 1 || dayOfMonth > 31) {
            dayOfMonth = Number.NaN
            alert("Day of Month must be between 1 and 31.")
         }
         Document.write("Value of dayOfMonth : " + dayOfMonth );
      }
   </script>
</head>
<body>
   <p>Click the following to see the result:</p>
   <form>
      <input type="button" value="Click Me" onclick="showValue();" />
   </form>
</body>
</html>

Example

Let’s create a function, sum which takes two parameters and converts them into Integers so that if users enter a Number in decimal or a Number in a string it will automatically change it into integers and sum those integers and print the value. Then we will call the function by passing some arguments.

<html>
<body>
   <h2> NaN property of a Number object in JavaScript </h2>
   <div id = "output"> </div>
   <script>
      function sum(a, b) {
         x = parseInt(a);
         y = parseInt(b);
         result = x + y;
         return result;
      }
      let outputDiv = document.getElementById("output");
      outputDiv.innerHTML += "Sum of 2 and 4.0 = " + sum(2, 4.0) + "<br>";
      outputDiv.innerHTML += "Sum of 2 and 4 = " + sum("2", 4) + "<br>";
      outputDiv.innerHTML += "Sum of Two and 4 = " + sum("Two", 4) + "<br>";
   </script>
</body>
</html>

Here when we either pass an Integer or a string containing Integers or Integers in the decimal value we will get the same results because all of the values can be parsed in integers and can be valid numbers but when we pass a string of alphabets we will get an error NaN, which means the value we passed is Not a Number.

Example

Let’s modify the above function, and we want this function to not allow the integers in decimal or even in the string, if the function gets a value that is strictly not a number type then we will console log the NaN to show a message that the entered number is not a number.

<html>
<body>
   <script>
      function sum(a, b) {
         if (typeof (a) === "number" && typeof (b) === "number") {
            document.write(a + b);
         } else {
            document.write(NaN);
         }
      }
      sum("2", 4);
   </script>
</body>
</html>

JavaScript can convert the numbers from one form to another by itself if it falls under the category of a number, it is not able to convert them into another required form, i.e., the entered number is not a number type in JavaScript, so JavaScript returns an error, NaN, it means not a number. The cool part is that the NaN is also an object of Number which we can access using the Number.NaN property. We can also manually throw the error NaN by using the Number.NaN property or directly passing NaN.

Updated on: 05-Jan-2023

426 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements