How To Convert A String To A Boolean In TypeScript

Convert a String to a Boolean in TypeScript

When working with strings, you might sometimes have to convert a string to a boolean in TypeScript. So in this tutorial, we will show you how to use the strict equality operator and some other methods to do it.

Convert a string to a boolean in TypeScript

Method 1: Use the strict equality operator

One trick we can use to solve this problem is to use the strict equality operator. Let’s say we have: 

var myString_1 = 'false';
var myString_2  = 'true';

These are the strings that we want to convert into booleans, which means that their value is true or false. This is basically how it works: 

myString_1 contains ‘false’, so we will use the strict equality operator to compare it with something else. Therefore, the operator will return false, and we will assign that to the result.   

var myBoo_1 = myString_1 === 'true';

myString_2 contains ‘true’, so we will compare it with ‘true’ so that it will return the ‘real’ true

var myBoo_2 = myString_2 === 'true';

Completed code:

var myString_1 = 'false';
var myString_2  = 'true';

var myBoo_1 = myString_1 === 'true';
var myBoo_2 = myString_2 === 'true';

console.log(myBoo_1);
console.log(typeof myBoo_1);
console.log(myBoo_2);
console.log(typeof myBoo_2);

Output: 

false
boolean
true
boolean

Method 2: Parse the string 

JavaScript has supported us with the JSON.parse() method to turn strings into objects.  

As TypeScript is a superset of JavaScript (the code you write in JavaScript will be basically runnable in TypeScript), we can use the JSON.parse() just the same as we would in JavaScript.

Syntax:

JSON.parse(text, reviver)

Parameters:

  • text: the string to parse
  • reviver (optional): a function that modifies the result before returning. 

Return value: Object, array, string, number, boolean, or null value based on the given JSON text.

Let’s apply that to solve our problem: 

var myString_1 = 'false';
var myString_2  = 'true';

var myBoo_1 = JSON.parse(myString_1);
var myBoo_2 = JSON.parse(myString_2);

console.log(myBoo_1);
console.log(typeof myBoo_1);
console.log(myBoo_2);
console.log(typeof myBoo_2);

Output: 

false
boolean
true
boolean

Method 3: Use the test() method 

The test() method will check for a match in a string.

Syntax:  

RegExpObject.test(string)

Parameters:

  • string: the string that is used to search.

Return value: Boolean, true if match, else false.  

Let’s use them to convert a string into boolean: 

var myString_1 = 'false';
var myString_2  = 'true';

var myBoo_1 = (/true/i).test(myString_1);
var myBoo_2 = (/true/i).test(myString_2); 

console.log(myBoo_1);
console.log(typeof myBoo_1);
console.log(myBoo_2);
console.log(typeof myBoo_2);

Output: 

false
boolean
true
boolean

Summary

This tutorial has described three common different ways to convert a string to a Boolean in TypeScript. You can use either the strict equality operator, the test() method, or parse the string.

Maybe you are interested:

Leave a Reply

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