How to convert a string to boolean in typescript [using 4 different ways]

In this typescript tutorial, we will see how to convert a string to boolean in typescript. Here are the examples we are going to cover:

  • Using the Boolean constructor
  • Using a ternary operator to check if the string equals “true”
  • Using the double exclamation mark !! operator
  • Using a switch statement

Using the Boolean constructor

Here we will see how to convert a string to a boolean using the boolean constructor in typescript.

The boolean constructor in typescript is used to convert a string to a boolean. The constructor takes a value as an argument and returns a boolean value. When a non-empty string value is passed, the returned boolean value will be true.

Open the code editor, create a file named as ‘convertStringToBoolean ‘, and then write the below code:

const str = 'true';
const boolValue = Boolean(str); // boolValue is true

To compile the code, run the below command and you can see the result in the console:

ts-node  convertStringToBoolean
convert a string to a boolean using the boolean constructor
convert a string to a boolean using the boolean constructor

This is an example of typescript for converting a string to a boolean using the boolean constructor.

Using a ternary operator to check if the string equals “true”.

Here we will see an example of how to convert a string to a boolean using a ternary operator to check the string equals “true” in typescript.

The ternary operator in typescript is a shorthand way of writing an if-else statement. It can also be used to convert a string to a boolean value by checking if the string is equal to “true” and returning true or false accordingly.

In the convertStringToBoolean.ts file, write the below code

const str = 'false';
const boolValue = str === 'false' ? true : false; 
console.log(boolValue);// boolValue is true

To compile the code run the below command and you can see the result in the console

ts-node convertStringToBoolean
convert a string to a boolean using a ternary operator to check the string equals true
convert a string to a boolean using a ternary operator to check the string equals true

This is an example of converting a string to a boolean using a ternary operator to check the string equals true in typescript.

Using the double exclamation mark !! operator

Here we will see an example of converting a string to a boolean using the double exclamation !! operator.

For example, we will take a string as”false”, it is converted to a boolean value using the double exclamation mark !! operator, which returns a boolean value of false.

In the convertStringToBoolean.ts file, write the below code:

const str = "false";
const boolValue = !!str;
console.log(boolValue);

To compile the code and run the below command and you can see the result in the console:

ts-node convertStringToBoolean
How to convert a string to boolean in typescript using double exclamation
How to convert a string to boolean in typescript using double exclamation

This is how we can convert a string to boolean in typescript using double exclamation.

Using a switch statement

Here we will see an example of converting a string to a boolean value, using a switch statement in typescript.

A switch statement in typescript can be used to check different cases of a string and convert it to a boolean value based on the case.

In the convertStringToBoolean.ts file, write the below code:

const str:string = 'false';
let boolValue: boolean;

switch(str) {
  case 'true':
    boolValue = true;
    break;
  case 'false':
    boolValue = false;
    break;
  default:
    throw new Error('Invalid string value');
}
console.log(boolValue);

To compile the code and run the below command and you can see the result in the console:

ts-node convertStringToBoolean
convert a string to a boolean value, using a switch statement in typescript
convert a string to a boolean value, using a switch statement in typescript

This is an example of how we can convert a string to a boolean value using a switch statement in typescript.

Conclusion

In this typescript tutorial, we saw how we can convert a string to a boolean value in typescript. Also, we saw different ways to convert a string to a boolean value:

  • Using the Boolean constructor
  • Using a ternary operator to check if the string equals “true”
  • Using a switch statement

You may like the following typescript tutorials:

>