While performing calculations in JS, there are instances when certain calculations result in unexpected or undefined outcomes. One such output is NaN. In this article, we learn about the NaN Property in JavaScript, Nan Values in Array, and how to understand it in mathematical operations.
What is NaN Property in JavaScript?
NaN is a special value in JavaScript that represents an undefined or unrepresentable number. It represents that the mathematical operation returns an invalid numeric result. NaN is short for “Not a Number”.
The type of NaN is a number only but it is different from other data types like integers, floating points, etc. Let’s check the type of NaN:
//checking the type of NaN console.log(typeof(NaN));
Output:
number
To check whether a value is NaN in JavaScript, we use the method Number.isNaN(). The method returns a boolean value. It returns true if the value is NaN and returns false, otherwise. This function is useful for checking whether a value is a valid number.
Check the examples below:
//checking if a number is NaN console.log(Number.isNaN(20)); //checking if a string is NaN console.log(Number.isNaN("hello")); //checking for NaN console.log(Number.isNaN(NaN));
Output:
false
false
true
If you want to debug where is the NaN coming in your code, you can use console.log
statements to print the values of variables and expressions at different points in your code. This way, you can trace back to the position which is resulting in NaN.
What are NaN Values in an Array?
Arrays are linear data structures that store homogenous data, i.e. same type of data. It can contain all integers, all characters, or all strings as the elements.
An array may also contain NaN values along with other defined values. It’s important to handle these values. Let us count the NaN values in an array. Here’s an example of the same.
//declaring an array let arr=[1,2,3,NaN,5,NaN]; //declaring count variable to count NaNs in Array let count=0; //looping through the array for(let i=0;i<arr.length;i++){ if(Number.isNaN(arr[i])){ count=count+1; } } //printing the count variable console.log(count);
Output:
2
In this example, we have two NaN values in the array. We have made a count variable to store the count of NaN values. We traverse the array using a for loop and check for every element for the NaN value. We use the method Number.isNaN() and increment the count variable for those elements that return true for the Number.isNaN() method. We have obtained the desired result.
Let us try to handle the NaN values in an array. Suppose we need the sum of an array. The NaN values will not contribute to the array’s sum, hence we have to handle them. Here’s an example of the same:
//declaring an array let arr=[1,2,3,NaN,5,NaN]; //declaring sum variable to sum up values of an array let sum=0; //looping through the array for(let i=0;i<arr.length;i++){ //handling the NaN values if(!Number.isNaN(arr[i])){ //summing up non-NaN values sum=sum+arr[i]; } } //printing the sum variable console.log(sum);
Output:
11
We used the “for” loop to traverse the array. To include only the non-NaN values in the sum variable, we use a condition to check for a number to be a NaN value. If the condition is true, we do not add in the sum, and if it is false, we add it in the sum variable.
In this example, we have used the condition “if an element is not a NaN value, add it to the sum variable”. Hence, we have obtained the desired result.
NaN Property in Mathematical Operations
There arise certain scenarios when performing mathematical operations we receive an invalid value. Let us see the examples to understand it better when we encounter NaN Property.
One such example could be dividing zero by zero:
//invalid operation let result=0/0; //printing the result console.log(result);
Output:
NaN
Dividing 0 by 0 is an ambiguity. Hence, it results in a NaN value.
Another example could be taking the square root of a negative number. This will also result in a NaN value.
//invalid operation let result=Math.sqrt(-3); //printing the result console.log(result);
Output:
NaN
The square root of negative numbers is not defined and is not possible. Hence the result will be a NaN value.
Let’s take another example. We will use the parseInt() function with string:
//invalid operation let result=parseInt("FavTutor"); //printing the result console.log(result);
Output:
NaN
We cannot convert a string to a number and hence it returns the NaN value, an undefined value.
Can we assign value to NaN in JavaScript?
If you assign some variable with NaN, you cannot assign a new value to it. This means that it is not a writable value. If you try to assign a value to NaN, the value of that variable will remain unchanged, there will be no change.
The next thing to learn is to convert strings to numbers in JavaScript.
Conclusion
Overall, NaN plays an essential role in JavaScript when dealing with invalid numbers. In this article, we explained the NaN Property and explored how they can exist in arrays as well as various mathematical operations that resulted in NaN values.