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 Infinity converted to Boolean in JavaScript?
In JavaScript, Infinity and -Infinity are special numeric values representing positive and negative infinity. When converting these values to Boolean, they always evaluate to true because they are considered "truthy" values.
This tutorial demonstrates three methods to convert Infinity values to Boolean in JavaScript:
- Using the Boolean() Method
- Using the Logical NOT(!) Operator
- Converting Infinity to String to Boolean
Using the Boolean() Method
The Boolean() constructor converts any value to its boolean equivalent. For Infinity and -Infinity, this method returns true since both are truthy values.
Syntax
Boolean(x);
Parameters
- x ? The value to be converted to Boolean
Example
<html>
<body>
<h2>Convert Infinity to Boolean</h2>
<div id="output"></div>
<script>
var mybool = Boolean(Infinity);
document.getElementById("output").innerHTML += "Boolean of +infinity: " + mybool + "<br>";
mybool = Boolean(-Infinity);
document.getElementById("output").innerHTML += "Boolean of -infinity: " + mybool;
</script>
</body>
</html>
Boolean of +infinity: true Boolean of -infinity: true
Using the Logical NOT(!) Operator
The double NOT operator !! is a shorthand way to convert values to Boolean. The first ! converts the value to Boolean and inverts it, while the second ! inverts it back to the original Boolean value.
Syntax
var x = !!(Infinity);
Example
<html>
<body>
<h2>Convert Infinity to Boolean</h2>
<div id="output"></div>
<script>
var val = !!Infinity;
document.getElementById("output").innerHTML += "Boolean of +infinity: " + val + "<br>";
val = !!-Infinity;
document.getElementById("output").innerHTML += "Boolean of -infinity: " + val;
</script>
</body>
</html>
Boolean of +infinity: true Boolean of -infinity: true
Converting Infinity to String to Boolean
This method first converts Infinity to a string, then converts that string to Boolean. Since non-empty strings are truthy in JavaScript, the result is true.
Syntax
let bool = !!(String(infinity));
Example
<html>
<body>
<h2>Convert Infinity to Boolean</h2>
<script>
var myVal = Infinity;
var myVal1 = String(myVal);
myVal = !!(myVal1);
let myBool = (myVal === (Boolean(myVal1)));
document.write("Boolean of +infinity: " + myBool + "<br>");
var val = -Infinity;
var val1 = String(val);
let val2 = !!(val1);
let bool = (val2 === (Boolean(val1)));
document.write("Boolean of -infinity: " + bool);
</script>
</body>
</html>
Boolean of +infinity: true Boolean of -infinity: true
Comparison
| Method | Syntax | Result |
|---|---|---|
| Boolean() | Boolean(Infinity) |
true |
| Double NOT | !!Infinity |
true |
| String to Boolean | !!(String(Infinity)) |
true |
Conclusion
All three methods convert Infinity and -Infinity to true because they are truthy values in JavaScript. The Boolean() method is the most straightforward and recommended approach for explicit type conversion.
