Home > Web Front-end > JS Tutorial > body text

How to convert NaN to 0 using JavaScript?

王林
Release: 2023-08-22 22:17:02
forward
1591 people have browsed it

How to convert NaN to 0 using JavaScript?

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

Use logical OR (||) operator

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.

grammar

NaN || 0 
Copy after login

Example

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> 
Copy after login

Use the double tilde (~~) operator

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.

grammar

~~NaN 
Copy after login

Example

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>
Copy after login

Use strict equality operator (===)

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.

grammar

(NaN === 0) ? NaN : 0
Copy after login

Example

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>
Copy after login

Use isNaN() function

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.

grammar

isNaN(value) ? 0 : value 
Copy after login

Example

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> 
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template