Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to convert NaN to 0 using JavaScript?
We can use the logical OR operator, double bitwise NOT operator, strict equality operator, or the isNaN() 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 different approaches to convert NaN to 0.
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 returns the first truthy value or the last value if all are falsy. Since NaN is falsy in JavaScript, using NaN || 0 will return 0.
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>
Result: 0
Using Double Tilde (~~) Operator
The double tilde or double bitwise NOT operator (~) performs a bitwise NOT operation twice. When applied to NaN, it returns 0 because NaN converts to 0 during bitwise operations.
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>
Result: 0
Using Strict Equality (===) Operator
The strict equality (===) operator compares both value and type. We can use it with a ternary operator to check if a value is NaN and return 0 if it is.
Syntax
(value !== value) ? 0 : value
Example
In this example we are converting NaN to 0 using strict equality operator. Note that NaN is the only value that is not equal to itself.
<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 (NaN is not equal to itself)
x = (x !== x) ? 0 : x;
// Print the value
document.write("Result: " + x);
</script>
</body>
</html>
Result: 0
Using isNaN() Function
The isNaN() function checks whether a value is NaN. We can use it with a ternary operator to return 0 when the value is NaN.
Syntax
isNaN(value) ? 0 : value
Example
In this example we are converting NaN to 0 using isNaN function.
<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>
Result: 0
Comparison
| Method | Syntax | Readability | Performance |
|---|---|---|---|
| Logical OR (||) | value || 0 |
High | Fast |
| Double Tilde (~~) | ~~value |
Low | Fast |
| Strict Equality | (value !== value) ? 0 : value |
Medium | Medium |
| isNaN() Function | isNaN(value) ? 0 : value |
High | Medium |
Conclusion
The logical OR operator (||) is the most commonly used and readable approach for converting NaN to 0. For explicit NaN checking, use isNaN() with a ternary operator for better code clarity.
