# How to convert Number to Boolean in JavaScript?

This tutorial will teach us to convert a number to a Boolean in JavaScript. The variable of Boolean data type can contain only two values, true and false. When we convert the variables of any other data type to Boolean, it returns true for all non-falsy values and false for all falsy values.

Let’s understand the falsy values. The JavaScript contains 6+ falsy values, and some of them are as below.

• Null
• 0
• NaN
• False
• Undefined
• ‘ ’

From the above falsy values, we can say that for 0, we will get the false Boolean value and for all other numbers, we will get the true value.

We will discuss the following approaches to convert numbers to Boolean.

• Using the Boolean() Function

• Using the Double Not (!!) Operator

## Using the Boolean() Function

In this method, we will use the Boolean() function, the built-in library function in JavaScript. It takes the variable as a parameter and returns true if variables don’t belong from the set of the falsy values. If the variable belongs to the set of the falsy group, it returns false.

### Syntax

Users can use the syntax below to convert the number to Boolean using JavaScript's Boolean() function.

let number  = 20;
let bool = Boolean( number );

### Parameters

• number − It can be a float type of number or integer; that the user wants to convert into Boolean values.

### Example

The below example demonstrates to convert the different values, including 0, float numbers, and integer values, to Boolean using the Boolean() function.

<html>
<body>
<h2>Convert the number to Boolean in JavaScript.</h2>
<h4> Using the <i> Boolean() </i> function to convert different number to Boolean. </h4>
<div id = "number1"> </div>
</body>
<script>
var number1 = document.getElementById("number1");
let number = 232.14;
let result = Boolean(number);
number1.innerHTML = "Boolean of 232.14 is: " + result + " <br/> ";
number = 0;
result = Boolean(number);
number1.innerHTML += "Boolean of 0 is : " + result + " <br/> ";
number = -2;
result = Boolean(number);
number1.innerHTML += "Boolean of -2 is : " + result + " <br/> ";
</script>
</html>


In the above output, users can observe that Boolean() function, returns false for the 0, and true for all other different numbers.

## Using the Double Not (!!) operator

When we use the unary Not (!) operator with the number values, it returns the Boolean value. If the number belongs to the set of falsy values, it returns true. To converge the returned values, users can use another Not operator.

### Syntax

Users can follow the below syntax to use the double Not (!!) operator with the number.

let number  = 0;
let bool = !!number;

### Example

We will convert the different number values, including negative and positive numbers, to Boolean using the double Not (!!) operator in the below example.

<html>
<body>
<h2> Convert the number to Boolean in JavaScript. </h2>
<h4> Using the <i> Double Not (!!) </i> operator to convert different number to Boolean. </h4>
<div id = "number1"> </div>
</body>
<script>
var number1 = document.getElementById("number1");
let number = 0;
let result = Boolean(number);
number1.innerHTML = "Boolean of 0 is: " + result + " <br/> ";
number = 1;
result = Boolean(number);
number1.innerHTML += "Boolean of 1 is : " + result + " <br/> ";
number = -34;
result = Boolean(number);
number1.innerHTML += "Boolean of -34 is : " + result + " <br/> ";
</script>
</html>


Users have learned two different approaches to converting the number to Booleans. There might be confusion that can occur while using the double Not operator for JavaScript beginners, but the first approach is straightforward.

Updated on: 10-Aug-2022

2K+ Views