If you’ve worked with TypeScript, you already know string and boolean are two primitive data types in TypeScript. In this article, we will show you how to convert a boolean to a string in TypeScript. We will introduce you to the method of casting right below. Stick with us in this article for detailed instructions.
Convert a boolean to a string in TypeScript
TypeScript provides you with many methods to work with String. Keep reading for some methods you can use to convert a boolean to a string in TypeScript.
Use the String()
function
Firstly, we will show you how to use the String()
function. Any value passed in the String()
function is converted to a string.
const strValue = String(100); console.log(strValue);
Output:
'100'
To illustrate, here is an example of how we apply it to the content we are interested in:
const boolValue = false; const strValue = String(boolValue); console.log(typeof strValue); console.log(strValue);
Output:
"string"
"false"
As a result, we get a new variable of type string and value ‘false’ in the output with a straightforward statement.
Use the toString()
method
Another common way is using the toString()
method. We call the method toString()
on a boolean variable to convert it to string.
For example, you can take a look at the example below:
const boolValue = false; const strValue = boolValue.toString(); console.log(typeof strValue); console.log(strValue);
Output:
"string"
"false"
Use the template literal
Template literal is delimited using the backticks. We can pass a variable or an expression into the `${expression}` syntax. Any value that we passed in that syntax will be converted to string.
const boolValue = false; console.log(typeof `${boolValue}`); console.log(`${boolValue}`);
Output:
"string"
"false"
Use the ternary operator
This is the same way you would use the if/else conditional block statement. Since a boolean value can only be ‘true’ or ‘false’, using this way is also quite convenient.
If the boolean value is ‘false’, then the string ‘false’ is returned. Otherwise, we will receive the string ‘true’.
const boolValue = false; const strValue = boolValue ? "true" : "false"; console.log(typeof strValue); console.log(strValue);
Output:
"string"
"false"
Summary
In summary, we have given you several methods to convert a Boolean to a String in TypeScript. The approaches using the String()
function and toString()
method are recommended because they are easy to understand. You can use whichever method you like. That’s all we want to convey. We hope this article is helpful to you.
Maybe you are interested:

My name’s Christopher Gonzalez. I graduated from HUST two years ago, and my major is IT. So I’m here to assist you in learning programming languages. If you have any questions about Python, JavaScript, TypeScript, Node.js, React.js, let’s contact me. I will back you up.
Name of the university: HUST
Major: IT
Programming Languages: Python, JavaScript, TypeScript, Node.js, React.js