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

We have a use case in development where strings with values like ‘true’ and ‘false’ are converted to a boolean datatype type.

We want to perform conditions - if-else on converting a boolean/string to display some results on the frontend.

This article, Publishing different ways to convert a string to a boolean or boolean in typescript.

We want string-formatted values to need to convert to a boolean value.

How to convert String to boolean in typescript

There are multiple following ways to convert String to boolean in typescript.

Using String equality example

For example, a String object holds a true or false value We will use a string equality check to convert this to 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 disadvantage of this approach is that it returns false for the string values ‘false’ and ‘asdfasdfasd’ string values.

Here false means we can’t identify with false as a boolean value or 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 regex pattern and test method. test() method is used to test match a pattern in a string if matched returned true, else returns false.

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

Convert String to boolean using typescript JSON Parse method example

It is one more way of converting boolean string values to string format.

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

We have written 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;
  }
}

How to convert boolean to string in typescript/javascript?

It is very easy to convert a Boolean to a string.

Typescript convert boolean to String using toString() method example

Any object in typescript/javascript has the toString() method which extends from the object and returns the string version.

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

Typescript convert boolean to String using ternary Operator example

It is a simple way to convert to a boolean string.

The ternary operator takes three arguments first condition if true returns the second argument, if false - returns the third argument.

Here is an example to do it

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

To Summarize, Learned about the conversion of A Boolean and strings in typescript.