How to Convert Boolean to String in TypeScript

To convert a boolean to a string in TypeScript, use the “String constructor”, “toString()”, “template literals”, or “ternary operator”.

Method 1: Using a String constructor

let boolValue: boolean = true;
let strValue: string = String(boolValue);

console.log(boolValue)
console.log(typeof boolValue)

console.log(strValue)
console.log(typeof strValue)

Output

Convert Boolean to String in TypeScript using String()

Method 2: Using toString()

let boolValue: boolean = true;
let strValue: string = boolValue.toString();

console.log(boolValue)
console.log(typeof boolValue)

console.log(strValue)
console.log(typeof strValue)

Output

Using the toString() Method

Method 3: Using template literals

let boolValue: boolean = true;
let strValue: string = `${boolValue}`;

console.log(boolValue)
console.log(typeof boolValue)

console.log(strValue)
console.log(typeof strValue)

Output

Using the template literals

Method 4: Using ternary operator

The ternary operator (conditional operator) is “used to convert a boolean to a string value based on its truthiness.”

let boolValue: boolean = true;
let strValue: string = boolValue ? 'true' : 'false'

console.log(boolValue)
console.log(typeof boolValue)

console.log(strValue)
console.log(typeof strValue)

Output

Using the ternary operator

This approach is more verbose than the previous ones, but it can be helpful in scenarios where you want to be explicit about the conversion or if you want to provide custom string values for true and false.

Related posts

TypeScript String to Number

TypeScript String to Enum

TypeScript Enum to String