Here's the scenario: you've just processed some data from a downstream service that, for whatever reason, gave you a boolean as a string.

In other words, you got "true" when you wanted true. Or you got "false" when you wanted false.

No worries. I can help you.

I'll show you how to convert string to boolean here.

In fact, I'll show you a few different ways to make that conversion.

Let's get going.

String Stuff

A simple string check will get the job done.

Take a look at this:

let str: string = "true";
let b: boolean = (str === "true"); 
console.log("boolean is " + b);

That's going to spit out true on the console.

Now let me explain what's going on.

The boolean string comes in as "true". You'd expect either that or "false".

The second line does the conversion. It sets the b variable to a boolean based on the value of the string.

Take a look at the contents of the parentheses in the second line. It's returning an actual boolean based on the value of the string.

If you're not familiar with the three (3) equals signs, that's a check not only for the value but also the type. In other words, both str and "true" must be of the same type (in this case, a string).

And since they're both strings and the value ("true") is equal, the expression returns true.

Now in that case, if the string is anything other than "true", the expression returns false. If you're good with that, then you can use this method.

Also: this method is case-specific. So if the value you get in is "True", the code above will return false and that's not what you want because you're a professional software developer who likes to get everything right.

Let's look at another method.

Getting Low

Let's look at a way to get around that case-sensitivity thing. Check out this code:

let str: string = "true";
let b: boolean = str.toLowerCase() === "true"
console.log("boolean is " + b);

That will give you the desired outcome.

But notice that it's converting the input string to lower case. That happens with the toLowerCase() method.

Then all you need to do is compare the resulting string against a lowercase version of true.

And that's what the code above does. And it works.

Regex

Yeah. You can use regex (regular expressions) for this.

Take a look at this example:

let str: string = "true";
let b: boolean = (/true/i).test(str)
console.log("boolean is " + b);

That will also spit out true on the console.

But a benefit of that method over the first method is that it ignores case.

So this will also work:

let str: string = "TrUe";
let b: boolean = (/true/i).test(str)
console.log("boolean is " + b);

Why will that work? Because of the i at the end of the regex. That tells the engine to ignore case.

And note that you don't put the /true/i in quotation marks. Lots of rookies make that mistake.

JSON

If neither of the above solutions suit your fancy, maybe you'd prefer to go with JSON.

let str: string = "true";
let b: boolean = JSON.parse(str);
console.log("boolean is " + b);

That'll do it.

Once again, though: it's case-sensitive. In fact, you'll get an error if you start messing around with the case in "true." So be careful with that.

But if you're expecting a completely lowercase stringified version of a boolean, that will do the trick.

Wrapping It Up

There you go. A few great ways to convert a string to a boolean.

Now it's up to you to pick the method you like best and start using it.

For my part, I like the regex method. It keeps the code concise while supporting case insensitivity.

But your mileage may vary.

Have fun!

Photo by Ketut Subiyanto from Pexels