Here are the five ways to convert a string to a boolean in TypeScript:
- Using ternary operator
- Using a boolean map
- Using JSON.parse() function
- Using direct comparison with Strict Equality
- Using the boolean object
- Using Double Negation
Method 1: Using the ternary operator
You can use the “ternary operator” to check if the string value is equivalent to a truthy string (e.g., “true”) and return the corresponding boolean value.
Example
let strValue: string = 'true';
const strToBool = (s: string): boolean => s.toLowerCase() === 'true' ? true : false;
const boolValue = strToBool(strValue)
console.log(strValue)
console.log(typeof strValue)
console.log(boolValue)
console.log(typeof boolValue)
Output
Method 2: Using a boolean map
You can create a mapping of string values to their boolean counterparts and then fetch the boolean value based on the input string.
Example
let strValue: string = 'true';
const strToBoolMap: { [key: string]: boolean } = {
'true': true,
'false': false
};
const strToBool = (s: string): boolean => strToBoolMap[s.toLowerCase()] || false;
const boolValue = strToBool(strValue)
console.log(strValue)
console.log(typeof strValue)
console.log(boolValue)
console.log(typeof boolValue)
Output
Method 3: Using JSON.parse() function
The JSON.parse() method is “used to convert certain string values to their respective boolean values.” This works best for “true” or “false” strings.
Example
let strValue: string = 'true';
const strToBool = (s: string): boolean => {
try {
return JSON.parse(s.toLowerCase());
} catch (error) {
return false;
}
};
const boolValue = strToBool(strValue)
console.log(strValue)
console.log(typeof strValue)
console.log(boolValue)
console.log(typeof boolValue)
Output
Method 4: Using direct comparison with Strict Equality
When you use strict equality to compare a string value, you check if the string is precisely equal to a specific value. In the case of boolean conversion, you’d typically check if the string is “true” or “false”.
Example
let strValue: string = 'true';
const strToBool = (s: string): boolean => s.toLowerCase() === 'true';
const boolValue = strToBool(strValue)
console.log(strValue)
console.log(typeof strValue)
console.log(boolValue)
console.log(typeof boolValue)
Output
Method 5: Using the Boolean object
Using the Boolean object to convert a string to a boolean in TypeScript (or JavaScript) can be misleading if not used carefully. By default, the Boolean object will convert any non-empty string to true and an empty string to false.
Example
let strValue: string = 'true';
const strToBool = (s: string): boolean => Boolean(s);
const boolValue = strToBool(strValue)
console.log(strValue)
console.log(typeof strValue)
console.log(boolValue)
console.log(typeof boolValue)
Output
Method 6: Using double negation
Double negation (!!) is a common JavaScript idiom to convert a value to its boolean representation. In TypeScript, which is a superset of JavaScript, this works similarly.
Example
let strValue: string = 'true';
const strToBool = (s: string): boolean => !!s;
const boolValue = strToBool(strValue)
console.log(strValue)
console.log(typeof strValue)
console.log(boolValue)
console.log(typeof boolValue)
Output
The behavior is similar to the direct use of the Boolean object. Any non-empty string is truthy in JavaScript, so it will be converted to true. An empty string is false, so it will be converted to false.
That’s it!
Related posts

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.