- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to convert NaN to 0 using JavaScript?
We can use the logical OR operator, double bitwise NOT operator, strict equality operator, or the inNaN() function to convert NaN to 0 using JavaScript. NaN in Javascript means Not a Number, whose type is Number but actually, it is not a number. In this article, we will learn to approaches to convert NaN to Boolean.
Using the Logical OR (||) Operator
Using the Double Tilde (~~) Operator
Using the Strict Equality (===) Operator
Using the isNaN() function
Using the Logical OR (||) Operator
The logical OR (||) operator is used to check the truthiness of two values. it will return true if any of those operands is true, else it returns false when all the operands are not true.
To convert NaN to 0 using || operator we simply compare NaN with 0 using || operator.
Syntax
NaN || 0
Example
In this example we are converting NaN to 0 using || operator.
<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>
Using Double Tilde (~~) Operator
The double tilde or double bitwise not operator is used to floor any positive number. We can also use Math.floor to do the same but this operator is faster than floor method. This operator works with only positive numbers and negative, in case if the number is NaN it returns zero.
Syntax
~~NaN
Example
In this example we are converting NaN to 0 using ~~ operator.
<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>
Using Strictly Equality (===) Operator
The strict equality (===) operator or strict equality operator is used to compare two or more operands by checking the equality between the values as well as their types. If the value and the type both are equal then it returns true, otherwise it returns false.
To convert NaN to zero using === operator, first we compare NaN value with zero and if it is true we return NaN otherwise we return zero.
Syntax
(NaN === 0) ? NaN : 0
Example
In this example we are converting NaN to 0 using strict equality operator.
<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>
Using isNaN() function
The isNaN function is used to check a value if it is NaN or not. To convert NaN to 0 we check the value if it is NaN or not. If true we return zero.
Syntax
isNaN(value) ? 0 : value
Example
In this example we are converting NaN to 0 using isNaN fuction..
<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>