JavaScript

How to Convert Numbers to Booleans in JavaScript

Type conversion is a phenomenon of converting one data type into another. Just like any other programming language, JavaScript provides two types of data conversions i.e. implicit and explicit. In implicit conversion JavaScript automatically convert one data type to another while in explicit type conversion we have to use some built-in functions to convert the data type.

The “number to Boolean” conversion belongs to the explicit type conversion. To convert a number to a Boolean data type we have to utilize a built-in function Boolean(). This write-up presents a detailed understanding of how to convert numbers to boolean in JavaScript. Afterward, it explains the impact of using not “!” sign and double not “!!” sign.

How to Convert Numbers to Booleans in JavaScript

Before jumping into the type conversion, first, we have to understand what is Boolean data type? Well! It is a very simple data type that has only two possible outcomes either true or false. Now the question is while converting the other data types to Boolean data type; when will it return true and when will it return a false value?

In JavaScript, the Boolean data type will convert all the values into true except the following values:

  • null
  • 0
  • NaN
  • false
  • ‘ ’
  • undefined

Now it’s time to understand how we can explicitly convert the numbers data type into Booleans data type. In JavaScript, Boolean function will return true for all the numeric values other than 0.

Example
The below-given code will show how to convert a Number into a Boolean value:

var a= 10;
console.log("The original Number: ", a);
console.log("Number converted to Boolean", Boolean(a));

In the above code we created a variable and assigned a number to it. To convert a numeric value to a Boolean value we utilized a built-in function “Boolean” and the console.log() function is used to print the original and the converted value of “a”. On successful execution of the code, we will get the following output on the browser’s console:

Example
Let’s consider another example to understand when Boolean will return the false value:

var a= 0;
console.log("The original Number: ", a);
console.log("Number converted to Boolean", Boolean(a));

Now the above code converts a numeric value “0” to a Boolean data type, as a result, it will return false as shown in the following snippet:

Use of “!” sign within the Boolean function provides a contradictory value i.e. Boolean function will show true for 0 and false for all non-zero values. Using two not signs “!!” will provide the actual results i.e. 0=false, 1=true.

Example
For better understanding consider the following piece of code:

var a= 0;
var b=10;
console.log("The original Number: ", a);
console.log("Number converted to Boolean", Boolean(!a));
console.log("The original Number: ", b);
console.log("Number converted to Boolean", Boolean(!b));

The above-given code provides the following output:

The above snippet verifies that use of “!” sign within the Boolean function shows the contrary results.

Conclusion

In JavaScript, a built-in function Boolean is used to convert the number data type to boolean data type. The Boolean function returns true for all the numeric values other than zero. However, the use of logical not operator within the Boolean function results in falsy results. This write-up presents a complete overview of how to convert a number data type to a Boolean data type. Moreover, it describes the consequences of using logical not operator “!” as well as double-negation “!!”.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.