The are multiple situations that when a boolean value of either True or False is passed via a JSON API or an environment variable such as a .env file, it gets returned as a string, which in some circumstances becomes a little difficult to work with.

In this post, we will be going over 5 ways to easily convert a string value of True or False into it’s correspond boolean.

1. Using JSON.parse

One very easy way is to use the parse method of JSON. JSON is a built-in object of javascript and its parse method parses a string as JSON and converts it into an object corresponding to the given text. So, if the string value is ‘true’, it is converted to a boolean object with value as true and false otherwise.

const trueValue = "true";
const boolValue = JSON.parse(trueValue);   //returns true

var falseValue = "false";
var boolValue = JSON.parse(falseValue);   //returns false

2. Using the comparison operator

With this approach, we are using the javascript equality operator to check the string value against a true value. If the string value is true, then the boolean value will also have a true, else a false as shown in the example below.

var stringValue = "true";
var boolValue = (stringValue =="true");   //returns true

3. Using a regular expression

Using the test method of javascript matches a regular expression against a string. In this case, we check whether the string contains true or not. Now, if the string value is true, then boolValue will be true, else a false. /i at the end of the regular expression is for case insensitive match.

var stringValue = "true";
var boolValue = (/true/i).test(stringValue) //returns true

4. Using ternary operator

In this next approach, we simply check the string for its equality with true and return a boolean true if there is a match and false otherwise using a ternary operator. This is actually just the shorthand of the comparison operator, but we thought it’s still worth calling it out.

var stringValue = "true";
var boolValue = stringValue.toLowerCase() == 'true' ? true : false;   //returns true

5. Using switch-case

Last but not least, we can use the switch-case control structure to cover all possible string value combinations which should lead to a true boolean value. Though we have used many string combinations for a true condition you may reduce them based on the string values stored by your application.

That is, if your application will only use the value true, then you don’t need other combinations such as 1, on etc. If the given string true matches, then the corresponding boolean is returned and boolean false is returned in every other case.

let booleanValue = null;
const stringValue = "true";
   switch(stringValue){
        case true:
        case "true":
        case 1:
        case "1":
        case "on":
        case "yes":
            booleanValue = true;
        default: 
            booleanValue = false;
    }
console.log(booleanValue);

We hope you enjoying going over these tips, feel to leave a comment or share these tips and tricks you use when working booleans and strings in other programming languages. We would also like to invite you to try out Calendarific.

Calendarific is a global event intelligence platform and a developer-friendly worldwide holiday API covering public, bank, local, religious holidays that you can use within your applications. Start using our API for free, also, did we mention that there’s no credit card required to start?



API icon

Start using our Holiday API

Use our Holiday data in your applications programmatically. No credit card is required to start.