# How Is Infinity converted to Boolean in JavaScript?

A number representing positive infinity is called infinity. -Infinity stands for the opposite of infinity. When a number exceeds its maximum value, it enters infinity: 1.797693134862315E+308. When an integer crosses its lower bound, it enters infinity: -1.797693134862316E+308. The universal object has the quality of infinity and is a variable with a global scope, in other words. The number is the beginning value of Infinity.

A Boolean value in JavaScript may either be true or false. Users can use the Boolean function to know if something is "true" or "false". Booleans can be kept track of and modified over time by being stored in variables. Booleans are utilized as functions to determine the values of variables, objects, conditions, and expressions. Booleans are essential for conditionals to function.

This tutorial will teach how infinity is converted to Boolean in JavaScript. Furthermore, we have different methods to achieve our goal in this tutorial.

• Using the Boolean() Method
• Using the Logical NOT(!) Operator
• Converting Infinity to String to Boolean

## Using the Boolean() Method

In this approach, we will see how we can use the Boolean object to convert infinity values to Boolean. If necessary, the value supplied as the first parameter is changed to a Boolean value. The object has a default value of false if the value is omitted, 0, -0, null, false, NaN, undefined, or the empty string (""). Any other value, such as an empty array ([]) or the word "false," creates an object with the value true as its initial value. True and False are not confused with the true and false values of the Boolean object or the primitive Boolean values.

### Syntax

Users can follow the below syntax to use the Boolean() method.

Boolean( x );


### Parameters

• x − The parameter passed to the Boolean object whose boolean value will be determined.

### Example

The example below demonstrates how to use the Boolean() object to convert infinity values to a boolean. Here, we declare two objects, myVal and Val, which take two values, infinity and –infinity. Both these are “truthy” values and return true when passed through the Boolean object.

<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>


In the above output, users can see that the value of +infinity in Boolean is true, and that of –infinity is also true. These values have been obtained from the Boolean object.

## Using the Logical NOT(!) operator

We will use the Logical NOT(!) operator in this method. The exclamation points are considered "unary operators". In essence, it means "not". The "!=" stands for "not equal to". The inverse of ==, which denotes equality, is this. Since it inverts logical (true/false) operators, it is typically employed with them. Thus, the converse may be obtained by putting an exclamation point before any function that returns TRUE.

### Syntax

Users can follow the syntax below to use the NOT(!) operator to convert Infinity values to Boolean values.

var x = !!(Infinity);


### Example

In the below example, we have used the NOT operator twice so that the actual Boolean value of the variable is maintained. The variables a and b store the Boolean values of the +infinity and -infinity.

<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>


In the above output, users can see that the value of +infinity in Boolean is true and that of –infinity is also true. These values have been obtained from the NOT operator.

## Converting Infinity to String to Boolean

In this method, we see how Infinity is converted to String and then to Boolean. A series of one or more characters-which might be letters, numbers, or symbols-is referred to as a string. Strings are immutable basic data types in JavaScript, which implies they cannot be changed. We will use the double Not(!!) operator and strict equality operator with the string value of Infinity to convert it to Boolean.

Users can follow the syntax below.

### Syntax

let bool = (!!(String(infinity)) === (Boolean(Infinity)));


### Example

In this example, we see that infinity is stored in a variable and then converted to a string. That variable is being converted to boolean and then checked whether the earlier value made boolean is matching with the present converted value. This value returns that both values are the same and the Boolean value is printed. The same process is repeated with –infinity until the Boolean value is printed.

<html>
<body>
<h2> Convert Infinity to Boolean </h2>
<script>
var myVal = Infinity;
var myVal1 = String(myVal);
myVal = !!(myVal1);
let myBool = (myVal === (Boolean(myVal1))); // true
document.write("Boolean of +infinity: " +(myBool));
document.write("<br>")
var val = -Infinity;
var val1 = String(val);
let val2 = !!(val1)
let bool = (val2 === (Boolean(val1))); // true
document.write("Boolean of -infinity: " + (bool));
</script>
</body>
</html>


In the above output, users can see that the value of +infinity in Boolean is true and that of –infinity is also true. These values have been obtained by converting the Infinity values first to string and then to Boolean. The Boolean value has been checked to see if both are true or false.

In this tutorial, we used three approaches to convert Infinity values to Boolean values. The first method focuses on how the simple Boolean() object can convert the infinity values to Boolean values. The second sheds light on the NOT operator. We use the Not operator twice to see how the Boolean values are retained. The third method showcases how an infinity value is converted to a string and Boolean. The user can use any method to convert infinity to Boolean in JavaScript.