How To Convert A Value To A Boolean In TypeScript

How to convert a Value to a Boolean in TypeScript

When working in TypeScript, if you are struggling and don’t know how to convert a value to a boolean in TypeScript, let’s follow our instructions below. We will show you some of the most common methods to get it done.

How to convert a value to a boolean in TypeScript

Using the Boolean() function

Syntax

Boolean(variable/statement)

The Boolean() function will check if a variable or a statement is true or false. 

In Python, only these values: ‘False‘, ‘ “” ‘ (empty string), ‘0‘, ‘undefined‘, ‘null‘, ‘NaN‘ will be returned false when using the Boolean() method. 

const myBool = false; 
console.log(Boolean(myBool));     // -> false

const myString = "";
console.log(Boolean(myString));   // -> false

const myNum = 0;
console.log(Boolean(myNum));      // -> false

const value_1 = undefined;
console.log(Boolean(value_1));    // -> false

const value_2 = null;
console.log(Boolean(value_2));    // -> false

const value_3 = NaN;
console.log(Boolean(value_3));    // -> false

All of the other values will be returned true.

const myBool = true; 
console.log(Boolean(myBool));                     // -> true

const myString = "Welcome to LearnShareIT.com";
console.log(Boolean(myString));                   // -> true

const myNum = 2111;
console.log(Boolean(myNum));                      // -> true

const myArr = [1, 3 ,4, 9, 5, 75, 23, 10];
console.log(Boolean(myArr));                      // -> true

Output:

true
true
true
true

Using the double bang operator

Another way to convert is to use a double band operator (!!). The double band operator is two logical not operators. The idea is: when you use the logical not operator, it will convert your value into boolean and invert it. For example: 

var myStr = "Welcome to LearnShareIT.com";
var result = !myStr;
console.log(result);

Output:

false 

Therefore, when using it one more time, we will have the correct Boolean value of the original value. For example: 

const myString = "";
console.log(!!myString);                     // -> false

const myNum = 0;
console.log(!!myNum);                        // -> false

const myArr = [1, 3 ,4, 9, 5, 75, 23, 10];
console.log(Boolean(myArr));                 // -> true

const value1 = undefined;
console.log(!!value1);                      // -> false

const value2 = null;
console.log(!!value2);                      // -> false

const value3 = NaN;
console.log(!!value3);                      // -> false

const myBool1 = true; 
console.log(!!myBool1);                     // -> true

const myString1 = "Welcome to LearnShareIT.com";
console.log(!!myString1);                   // -> true

Output:

false 
false 
true 
false 
false 
false 
true 
true 

Summary

In this tutorial, we have demonstrated how to convert a value to a boolean in TypeScript. You can use both the Boolean() method or the double bang operation (!!). But the Boolean() method is more preferable and more widely used. 

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *