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 is NaN converted to Boolean in JavaScript?
In JavaScript, NaN (Not a Number) is a special numeric value that represents an invalid number. When converted to Boolean, NaN is always considered a falsy value. This article explores three methods to convert NaN to Boolean.
- Using the Boolean() Function
- Using the !! Operator
- Using the NOT Operator and isNaN() Method
Understanding NaN as a Falsy Value
In JavaScript, NaN is one of the falsy values (along with false, 0, "", null, and undefined). This means it always converts to false in Boolean contexts.
Using the Boolean() Function
The Boolean() function converts any value to its Boolean equivalent. When passed NaN, it returns false.
Syntax
Boolean(NaN)
Example
<html>
<body>
<p>Convert NaN to Boolean</p>
<p id="result"></p>
<script>
let bool = Boolean(NaN);
document.getElementById("result").innerHTML += "Boolean(NaN): " + bool + "<br>";
document.getElementById("result").innerHTML += "Type: " + typeof bool;
</script>
</body>
</html>
Boolean(NaN): false Type: boolean
Using the !! Operator
The double NOT operator (!!) is a shorthand way to convert values to Boolean. The first ! converts to Boolean and negates it, the second ! negates it back to the correct Boolean value.
Syntax
!!value
How It Works
<html>
<body>
<p id="output"></p>
<script>
let x = "Tutorials Point";
let x1 = !x; // false (negated truthy value)
let x2 = !!x; // true (double negation)
document.getElementById("output").innerHTML = "!x: " + x1 + "<br>";
document.getElementById("output").innerHTML += "!!x: " + x2;
</script>
</body>
</html>
!x: false !!x: true
Converting NaN with !! Operator
<html>
<body>
<p>Convert NaN to Boolean</p>
<p id="output"></p>
<script>
let bool = !!NaN;
document.getElementById("output").innerHTML += "!!NaN: " + bool + "<br>";
document.getElementById("output").innerHTML += "Type: " + typeof bool;
</script>
</body>
</html>
!!NaN: false Type: boolean
Using the NOT Operator and isNaN() Method
The isNaN() method returns true if the value is NaN, otherwise false. Combined with the NOT operator (!), we can convert this to the opposite Boolean value.
Syntax
!isNaN(NaN)
Example
<html>
<body>
<p>Convert NaN to Boolean using isNaN()</p>
<p id="output"></p>
<script>
let bool = !isNaN(NaN);
document.getElementById("output").innerHTML += "!isNaN(NaN): " + bool + "<br>";
document.getElementById("output").innerHTML += "Type: " + typeof bool + "<br>";
document.getElementById("output").innerHTML += "isNaN(NaN): " + isNaN(NaN);
</script>
</body>
</html>
!isNaN(NaN): false Type: boolean isNaN(NaN): true
Comparison of Methods
| Method | Result for NaN | Performance | Readability |
|---|---|---|---|
Boolean(NaN) |
false | Good | Excellent |
!!NaN |
false | Slightly faster | Good |
!isNaN(NaN) |
false | Slower | Good |
Key Points
-
NaNis a falsy value in JavaScript - All three methods convert
NaNtofalse - The
!!operator is slightly faster thanBoolean() -
Boolean()is the most readable approach - The
isNaN()method approach is primarily useful for explicit NaN checking
Conclusion
NaN always converts to false when converted to Boolean. The Boolean() function is the most readable approach, while !! offers slightly better performance for frequent conversions.
