How to Convert String to/from A Boolean in typescript?

We are currently working on a use case in development where strings containing values such as true and false are being converted to a boolean data type.

We are currently working in a development use case where strings, containing values like true and false, needs conversion to a boolean data type.

Some times, these strings hold boolean values that we need for performing conditional if-else logic to display results based on certain conditions.

To achieve this, we must convert strings to booleans or vice versa.

This article explores various methods for converting a string to a boolean or a boolean in TypeScript.

Our aim is to convert string-formatted values into their corresponding boolean representations.

How to convert String to boolean in typescript

There are multiple ways to convert a String to a boolean in TypeScript.

Using String Equality Example

For instance, a String object may hold a true or false value. We can use a string equality(===) check to convert it into a Boolean.

let inputTrueString = "true";
let inputFalseString = "true";
let inputNotBooleanString = "asdfasdfasd";
let truthValue = inputTrueString === "true";
let falseValue = inputFalseString === "false";
let notBooleanValue = inputNotBooleanString === "false";
console.log(truthValue); // outputs true
console.log(falseValue); // outputs false
console.log(notBooleanValue); // outputs false

The only drawback of this approach is that it returns true for the string values false and asdfasdfasd. In this context, true does not precisely indicate whether it is a boolean value or a string value.

This will be helpful in conditional logic validations for verifying the string to boolean conversion.

Using Regular Expression Example

We can convert to boolean using a regex pattern and the test method.

The test() method checks if a pattern in a string matches and returns true if there’s a match, otherwise, it returns false.

/true/i
  .test("true")(
    //outputs true
    /true/i,
  )
  .test("false")(
    //outputs false
    /true/i,
  )
  .test("asdfasdfads"); //outputs false

JSON Parse method example

This is another method for converting boolean string values to a boolean format.

The JSON parse method accepts a string and returns true if the value is true, false if the value is false, and throws an exception if the value is any other string than boolean values.

We’ve implemented a function to return undefined for normal strings.

console.log(stringToBoolean("true")); // undefined
console.log(stringToBoolean("false")); // outpus undefined
console.log(stringToBoolean("asdfasdfasd")); //outputs undefined
function stringToBoolean(stringValue: string): boolean | undefined {
  try {
    return JSON.parse(stringValue);
  } catch (e) {
    return undefined;
  }
}

These methods provide different ways to handle the conversion of string values to booleans, each with its own characteristics and use cases.

How to Convert Boolean to String in TypeScript/JavaScript?

Converting a Boolean to a string is a straightforward process.

Using toString() method example

In TypeScript/JavaScript, any object possesses the toString() method, inherited from the object, which returns its string representation.

let booleanTrueVariable: boolean = true;
let booleanFalseVariable: boolean = false;
let stringTrue: string = booleanTrueVariable.toString();
let stringFalse: string = booleanFalseVariable.toString();
console.log(stringTrue); // outputs true
console.log(typeof stringTrue); // outputs string
console.log(stringFalse); // outputs false
console.log(typeof stringFalse); // outputs string

Using Ternary Operator Example

Another simple way to convert a boolean to a string is by using the ternary operator.

This operator takes three arguments: the first is the condition; if true, it returns the second argument; if false, it returns the third argument.

Here is an example

let booleanTrueVariable: boolean = true;
let booleanFalseVariable: boolean = false;
let stringValue = booleanTrueVariable ? "true" : "false";
let stringTrue: string = booleanTrueVariable ? "true" : "false";
let stringFalse: string = booleanFalseVariable ? "true" : "false";
console.log(stringTrue); // outputs true
console.log(typeof stringTrue); // outputs string
console.log(stringFalse); // outputs false
console.log(typeof stringFalse); // outputs string

Conclusion

In summary, we have learned different methods for converting a boolean to/from a string in TypeScript/JavaScript.