We can use logical OR operator, double bitwise NOT operator, strict equality operator or inNaN() function to convert NaN to 0 using JavaScript. NaN in JavaScript means is not a number , its type is Number, but actually it is not a number. In this article, we will learn two methods to convert NaN to Boolean value.
Use logical OR (||) operator
Using the Double Tilde (~~) Operator
Use the strict equality operator (===)
Use isNaN() function
Logical OR (||) operator is used to check the authenticity of two values. Returns true if any of the operands are true, otherwise returns false if all operands are not true.
Use || operator to convert NaN to 0, we just use || operator to compare NaN with 0.
NaN || 0
In this example, we use the || operator to convert NaN to 0.
<html> <head> <title>Example- convert NaN to 0 using JavaScript</title> </head> <body> <h2 id="demo">Converting NaN to 0 using Javascript</h2> <script> // Defining Variables let x = NaN; let y = 0; // Convert NaN to 0 x = x || y; // Print the values document.write("Result: " + x); </script> </body> </html>
The double tilde or double-bit NOT operator is used to round down any positive number. We can also use Math.floor to achieve the same effect, but this operator is faster than the floor method. This operator only works with positive and negative numbers, returning zero in case the number is NaN.
~~NaN
In this example, we use the ~~ operator to convert NaN to 0.
<html> <head> <title>Example -convert NaN to 0 using JavaScript</title> </head> <body> <h3 id="demo">Converting NaN to 0 using Javascript Using double bitwise not operator</h3> <script> // Defining Variable let x = NaN; // Convert NaN to 0 x = ~~x // Print the value document.write("Result: " + x); </script> </body> </html>
Strict equality (===) operator or strict equality operator is used to compare two or more operands by checking equality between values and types. Returns true if both value and type are equal, false otherwise.
Convert NaN to zero using the === operator, first we compare the NaN value with zero, if true, return NaN, otherwise return zero.
(NaN === 0) ? NaN : 0
In this example, we use the strict equality operator to convert NaN to 0.
<html> <head> <title>Example - convert NaN to 0 using JavaScript</title> </head> <body> <h3 id="demo">Converting NaN to 0 using Javascript Using Strict equality operator</h3> <script> // Defining Variable let x = NaN; // Convert NaN to 0 x = (x === 0) ? x : 0; // Print the value document.write("Result: " + x); </script> </body> </html>
isNaN function is used to check whether a value is NaN. To convert NaN to 0, we check if the value is NaN. If yes, zero is returned.
isNaN(value) ? 0 : value
In this example, we use the isNaN function to convert NaN to 0.
<html> <head> <title>Example - convert NaN to 0 using JavaScript</title> </head> <body> <p id="demo">Converting NaN to 0 using Javascript Using isNaN function</p> <script> // Defining Variable let x = NaN; // Convert NaN to 0 x = isNaN(x) ? 0 : x; // Print the value document.write("Result: " + x); </script> </body> </html>
The above is the detailed content of How to convert NaN to 0 using JavaScript?. For more information, please follow other related articles on the PHP Chinese website!