4 Ways to Convert Strings to Booleans in TypeScript
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
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
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.

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
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
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
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.

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
You can use both the Boolean object and double negation to convert any value into a boolean, not just strings.
Unlimited access to hundred of tutorials
Access to exclusive interactive lessons
Remove ads to learn without distractions