How to Convert Boolean to String in TypeScript?

In this tutorial, I will explain how to convert a boolean to a string in TypeScript and show you various methods with examples.

Convert Boolean to String in TypeScript

Boolean values (true or false) are often used for conditional logic in TypeScript. However, there are scenarios where you need to convert these boolean values into strings. For instance:

  • Sending data to a server that expects string values.
  • Displaying boolean values in a user interface.
  • Logging boolean values for debugging purposes.

Methods to Convert Boolean to String in TypeScript

There are several methods to convert a boolean to a string in TypeScript. Let’s explore each method with examples.

Using the toString() Method

The toString() method is the most straightforward way to convert a boolean to a string. This method is built into JavaScript and TypeScript, making it a reliable option.

let isAvailable: boolean = true;
let availabilityString: string = isAvailable.toString();
console.log(availabilityString); // Output: "true"

In this example, isAvailable is a boolean variable that we convert to a string using the toString() method. The resulting string is "true".

I executed the above TypeScript code and you can see the exact output in the screenshot below:

SyntaxError invalid character in an identifier

Check out How to Split a String by Comma in TypeScript?

Using Template Literals

Template literals are another elegant way to convert a boolean to a string. This method is particularly useful when you need to embed the boolean value within a larger string.

let isRegistered: boolean = false;
let registrationStatus: string = `${isRegistered}`;
console.log(registrationStatus); // Output: "false"

Here, we use backticks to create a template literal, embedding the boolean value directly into the string. This approach is concise and readable.

Using String Concatenation

String concatenation is a simple and effective method to convert a boolean to a string. You can easily achieve the conversion by concatenating the boolean value with an empty string.

let isActive: boolean = true;
let activeStatus: string = isActive + '';
console.log(activeStatus); // Output: "true"

This method leverages the type coercion feature of JavaScript, converting the boolean to a string during the concatenation process.

Read How to Convert an Array to a String in TypeScript?

Using the JSON.stringify() Method

The JSON.stringify() method is another powerful tool for converting boolean values to strings. This method is particularly useful when working with JSON data.

let isVerified: boolean = false;
let verificationString: string = JSON.stringify(isVerified);
console.log(verificationString); // Output: "false"

In this example, JSON.stringify() converts the boolean value isVerified to its string representation "false".

Here is the exact output in the screenshot below:

SyntaxError invalid character in identifier in Python

Using Conditional (Ternary) Operator

The conditional (ternary) operator clearly and explicitly converts a boolean to a string based on its value in TypeScript.

let hasAccess: boolean = true;
let accessString: string = hasAccess ? 'true' : 'false';
console.log(accessString); // Output: "true"

This method is particularly useful when handling more complex logic during the conversion process.

Check out Convert Date to String Format DD/MM/YYYY in TypeScript

Real-World Example: User Registration System

Now, let me show you a real example to help you understand this better.

Let’s consider a real-world example where we need to display user registration status on a website. We have a boolean value indicating whether a user is registered and we need to convert this value to a string for display purposes.

interface User {
    name: string;
    isRegistered: boolean;
}

let user: User = {
    name: 'Emily Johnson',
    isRegistered: true
};

let registrationMessage: string = `${user.name} is ${user.isRegistered ? 'registered' : 'not registered'}.`;
console.log(registrationMessage); // Output: "Emily Johnson is registered."

In this scenario, we use a combination of template literals and the conditional operator to create a user-friendly message based on the boolean value.

Performance Considerations

When choosing a method to convert boolean values to strings in TypeScript, it’s important to consider performance, especially in applications with high-frequency conversions. Among the methods discussed, toString() and template literals are generally the most performant due to their simplicity and directness.

Conclusion

In this tutorial, I explained how to convert boolean values to strings in TypeScript using various methods. With examples, we discussed the below method:

  • Using the toString() Method
  • Using Template Literals
  • Using String Concatenation
  • Using the JSON.stringify() Method
  • Using Conditional (Ternary) Operator

I hope this tutorial helps you understand converting boolean values to strings in TypeScript. If you have any questions or need further assistance, feel free to leave a comment below.

You may also like:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.