πŸ’‘ This page contain affiliate links. By making a purchase through them, we earn a commission at no extra cost to you.

4 Ways to Convert Strings to Booleans in TypeScript

Ferenc Almasi β€’ 2022 September 19 β€’ πŸ“– 4 min read

To convert a string to a boolean in TypeScript, the recommended way is to wrap your value into the built-in JSON.parse function which will convert it into a boolean and return either true or false, depending on the input.

const booleanTrue: boolean  = JSON.parse('true')  // Returns true
const booleanFalse: boolean = JSON.parse('false') // Returns false
Copied to clipboard!

You could also create a helper function and return true or falseΒ manually, based on the provided argument.

const booleanify = (value: string): boolean => {
    const truthy: string[] = [
        'true',
        'True',
        '1'
    ]

    return truthy.includes(value)
}

booleanify('true')   // Returns true
booleanify('True')   // Returns true
booleanify('1')      // Returns true
booleanify('false')  // Returns false
booleanify('0')      // Returns false
Copied to clipboard!

Here we can utilize the includes method on an array of strings. This way, we can also cater to capitalized strings, or evaluate "1" as true. Anything that is included in the array will return true, everything else will return false.

There are of course many different ways to achieve the same thing. So in case for whatever reason, you would like to go with a different solution, here are 3 other ways to convert strings to booleans in TypeScript in order of recommendation.

Looking to improve your skills? Master TypeScript from start to finish.
Master TypeScript

Using Strict Equality

There is also the strict equality operator which you can use to check if your string values are equal to either "true" or "false" and create a boolean from that. Note that you might want to convert your string to lowercase first, in case you receive the text capitalized, otherwise you may end up with an incorrect value.

const stringTrue: string = 'true'
const stringCapitalizted: string = 'True'

const booleanTrue: boolean = stringTrue === 'true' // Returns true
const booleanFalse: boolean = stringCapitalizted === 'true' // Returns false
const booleanLowercase: boolean = stringCapitalizted.toLowerCase() === 'true' // Returns true
Copied to clipboard!

Strict equality checks the type and value of the variable whereas the double equal operator only checks the value. Because of this, you always want to use strict equality in your code, otherwise, you may end up with false positives.

// ❌ Returns true, as only the value is checked
'1' == 1

// βœ”οΈ Returns false, as both value and type are checked
'1' === 1
Copied to clipboard!

Using the Boolean Object

There are two more ways to convert strings to booleans, however, due to their nature, they will produce false positives. One of them could be achieved by wrapping your value into aΒ BooleanΒ object which will convert your value into a boolean and return eitherΒ trueΒ orΒ falseΒ if an empty string is provided.

const booleanTrue: boolean  = Boolean('true')   // Returns true
const booleanFalse: boolean = Boolean('false')  // Returns true
const booleanOne: boolean   = Boolean('1')      // Returns true
const booleanZero: boolean  = Boolean('0')      // Returns true
const booleanSpace: boolean = Boolean(' ')      // Returns true
const booleanEmpty: boolean = Boolean('')       // Returns false
Whitespaces also have length which makes them result in true
Copied to clipboard!

Notice that all strings will return true, and only empty strings will return false in this case, as all strings that have a length are considered truthy values.

Looking to improve your skills? Master TypeScript from start to finish.
Master TypeScript

Using Double Negation

Lastly, another way to convert strings to booleans is to use a double negation. Just like with the Boolean object, you will get the same behavior where all strings will be true, regardless of their values, expect an empty string.

const booleanTrue: boolean  = !!'true'   // Returns true
const booleanFalse: boolean = !!'false'  // Returns true
const booleanOne: boolean   = !!'1'      // Returns true
const booleanZero: boolean  = !!'0'      // Returns true
const booleanSpace: boolean = !!' '      // Returns true
const booleanEmpty: boolean = !!''       // Returns false
Copied to clipboard!

You can use both the Boolean object and double negation to convert any value into a boolean, not just strings.

Did you find this page helpful?
πŸ“š More Webtips
Frontend Course Dashboard
Master the Art of Frontend
  • check Unlimited access to hundred of tutorials
  • check Access to exclusive interactive lessons
  • check Remove ads to learn without distractions
Become a Pro

Recommended