In this tutorial, I will explain how to convert a string to a boolean in TypeScript. This is a common requirement in web development, especially when dealing with user inputs or data retrieved from APIs. Let me show you how to do this with some examples.
Methods to Convert String to Boolean in TypeScript
There are several methods to convert a string to a boolean in TypeScript. We’ll cover the most effective ones:
- Using Equality Comparison
- Using the Boolean Constructor
- Using JSON.parse()
- Using Conditional Statements
Using Equality Comparison
The simplest way to convert a string to a boolean in TypeScript is by using an equality comparison. This method checks if the string is equal to “true” (case-sensitive) and returns the corresponding boolean value.
Example:
function stringToBoolean(str: string): boolean {
return str === 'true';
}
const isRegistered = stringToBoolean('true'); // true
const isAdmin = stringToBoolean('false'); // false
console.log("isRegistered:", isRegistered);
console.log("isAdmin:", isAdmin);In this example, stringToBoolean function will return true if the input string is exactly “true” and false otherwise. This method is straightforward and works well for simple cases.
I executed the above TypeScript code using VS code and you can see the exact output in the screenshot below:

Check out Convert String to Number in TypeScript
Using the Boolean Constructor
Another method to convert a string to a boolean in TypeScript is to use the Boolean constructor. This method can be a bit tricky as it converts any non-empty string to true.
Example:
function stringToBoolean(str: string): boolean {
return Boolean(str);
}
const hasAccess = stringToBoolean('true'); // true
const isActive = stringToBoolean('false'); // true
const isVerified = stringToBoolean(''); // false
console.log("hasAccess:", hasAccess);
console.log("isActive:", isActive);
console.log("isVerified:", isVerified);Here, the Boolean constructor converts any non-empty string to true and an empty string to false. This method is useful when you want to check if the string is non-empty rather than its specific value.
Here is the exact output in the screenshot below:

Check out Check if a String Contains a Substring in TypeScript
Using JSON.parse()
The JSON.parse() method can also be used to convert a string to a boolean in TypeScript. This method is particularly useful when dealing with JSON data. Let me show you an example.
Example:
function stringToBoolean(str: string): boolean {
try {
return JSON.parse(str.toLowerCase());
} catch (e) {
return false;
}
}
const isSubscribed = stringToBoolean('true'); // true
const isPremium = stringToBoolean('false'); // false
const isTrial = stringToBoolean('TRUE'); // true
const isExpired = stringToBoolean('FALSE'); // false
console.log("isSubscribed:", isSubscribed);
console.log("isPremium:", isPremium);
console.log("isTrial:", isTrial);
console.log("isExpired:", isExpired);In this example, JSON.parse is used to convert the string to a boolean. We use toLowerCase() to handle case insensitivity. If the string is not a valid JSON boolean, it catches the error and returns false.
Here is the exact output in the screenshot below:

Read Convert Date to String in TypeScript
Using Conditional Statements
For more complex scenarios, you might want to use conditional statements to handle different string values.
Example:
function stringToBoolean(str: string): boolean {
switch (str.toLowerCase()) {
case 'true':
case 'yes':
case '1':
return true;
case 'false':
case 'no':
case '0':
return false;
default:
return false;
}
}
const isEligible = stringToBoolean('yes'); // true
const isComplete = stringToBoolean('no'); // false
const isConfirmed = stringToBoolean('1'); // true
const isDenied = stringToBoolean('0'); // falseIn this example, we use a switch statement to handle different string representations of boolean values. This method is flexible and allows you to define custom logic for various string inputs.
Read Convert String to Date in TypeScript
Practical Use Cases and Example
Now, let me show you a practical example and an actual use case of converting a string to a boolean in TypeScript.
User Registration Form
Imagine you have a user registration form where users indicate if they agree to the terms and conditions. This data is sent to your backend as a string.
Example:
interface User {
name: string;
agreeToTerms: boolean;
}
function stringToBoolean(str: string): boolean {
switch (str.toLowerCase()) {
case 'true':
case 'yes':
case '1':
return true;
case 'false':
case 'no':
case '0':
return false;
default:
return false;
}
}
function registerUser(name: string, agreeToTermsStr: string): User {
const agreeToTerms = stringToBoolean(agreeToTermsStr);
return { name, agreeToTerms };
}
const newUser = registerUser('John Doe', 'true');
console.log(newUser); // { name: 'John Doe', agreeToTerms: true }In this example, we use the stringToBoolean function to convert the agreeToTermsStr to a boolean before creating the User object.
You can see the exact output in the screenshot below:

API Response Handling
When dealing with API responses, you might receive boolean values as strings. Converting these strings to boolean values is crucial for further processing.
Example:
interface ApiResponse {
success: boolean;
message: string;
}
function handleApiResponse(response: { success: string, message: string }): ApiResponse {
const success = stringToBoolean(response.success);
return { success, message: response.message };
}
const apiResponse = handleApiResponse({ success: 'true', message: 'Operation completed successfully.' });
console.log(apiResponse); // { success: true, message: 'Operation completed successfully.' }In this example, we convert the success string from the API response to a boolean before using it in our application.
Conclusion
In this tutorial, I explained how to convert strings to boolean values in TypeScript using different methods such as using the equality comparison, the Boolean constructor, JSON.parse(), or conditional statements, etc. I have also shown you some real examples of converting a string to a boolean in TypeScript.

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.