Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • TypeScript Tutorial
  • TS Exercise
  • TS Interview Questions
  • TS Cheat Sheet
  • TS Array
  • TS String
  • TS Object
  • TS Operators
  • TS Projects
  • TS Union Types
  • TS Function
  • TS Class
  • TS Generic
Open In App
Next Article:
How to Convert a String to enum in TypeScript?
Next article icon

How to Convert a Bool to String Value in TypeScript ?

Last Updated : 07 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

When we talk about converting a boolean to a string value in TypeScript, we mean changing the data type of a variable from a boolean (true or false) to a string (a sequence of characters). We have multiple approaches to convert a bool to a string value in TypeScript.

Example: let's say you have a variable isLogged that represents whether a user is logged in or not:

const isLogged: boolean = true;

If you need to use this boolean value as a string, you can convert it to a string representation using various methods, as discussed earlier. The result would be a new variable with a string data type:

const isLogged: boolean = true; const isLoggedAsString: string = isLogged.toString();

Now, isLoggedAsString holds the string representation of the boolean value true as "true". This can be useful when you need to concatenate it with other strings, display it in a user interface, or pass it to functions or APIs that expect string values.

Below are the approaches used to Convert a bool to a string value in TypeScript:

Table of Content

  • Using Ternary Operator
  • Using Template Literal
  • Using toString()
  • Using String Constructor
  • Using JSON.stringify()
  • Using concat() Method

Approach 1: Using Ternary Operator

The ternary operator is a concise way to write a conditional expression. It allows you to check a condition and choose between two values based on whether the condition is true or false.

Example: In this example, the ternary operator checks if myBool is true. If true, it assigns the string 'true'; otherwise, it assigns 'false'.

JavaScript
const myBool: boolean = true; const boolAsString: string = myBool ? 'true' : 'false'; console.log(boolAsString); // Outputs 'true' or 'false' 

Output:

true

Approach 2: Using Template Literal

Template literals are a convenient way to create strings in TypeScript. When you enclose an expression within ${}, it gets evaluated and included in the resulting string.

Example: In this example, the expression ${myBool} is evaluated, and the result is a string containing the boolean value converted to its string representation.

JavaScript
const myBool: boolean = true; const boolAsString: string = `${myBool}`; console.log(boolAsString); // Outputs 'true' or 'false' 

Output:

true

Approach 3: Using toString()

The toString() method is available on primitive values, including booleans. It converts the boolean to its string representation.

Example: In this example, the toString() method is called on myBool, returning a string representation of the boolean value.

JavaScript
const myBool: boolean = true; const boolAsString: string = myBool.toString(); console.log(boolAsString); // Outputs 'true' or 'false' 

Output:

true

Approach 4: Using String Constructor

The String constructor can also be employed to convert boolean values to their string representation. By passing a boolean value to the String constructor, it automatically converts it into its string equivalent.

Syntax:

let bool: boolean = true; let str: string = String(bool);

Example: In this example we converts a boolean value to its string representation using the String constructor and logs it. The result is either 'true' or 'false'.

JavaScript
const myBool: boolean = true; const boolAsString: string = String(myBool); console.log(boolAsString); 

Output:

true

Approach 5: Using JSON.stringify()

In this approach, we use the JSON.stringify() method to convert a boolean to its string representation. This method converts JavaScript values to a JSON string, making it a versatile option for handling various data types, including booleans.

Example: The following example demonstrates how to use JSON.stringify() to convert a boolean to a string in TypeScript.

TypeScript
const isLogged: boolean = true; const isLoggedAsString: string = JSON.stringify(isLogged);  console.log(isLoggedAsString); 

Output:

true

Using concat() Method

The concat() method can be used to concatenate an empty string to a boolean, converting it to a string.

Syntax:

let str: string = boolValue.concat("");

Example: In this example, concatenating an empty string ("") to the boolean converts it to a string.

JavaScript
const boolValue: boolean = true; const boolAsString: string = boolValue + ""; console.log(boolAsString); console.log(typeof boolAsString); 

Output
true string 

Next Article
How to Convert a String to enum in TypeScript?

A

amanv09
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • TypeScript

Similar Reads

  • How to Convert String to Boolean in TypeScript ?
    In Typescript, sometimes you receive the data as strings but need to work with boolean values or identify the boolean equivalent of it. There are several approaches to convert string to boolean in TypeScript which are as follows: Table of Content Using Conditional StatementUsing JSON.parse() MethodU
    4 min read
  • How to Convert a String to enum in TypeScript?
    In TypeScript, an enum is a type of class that is mainly used to store the constant variables with numerical and string-type values. In this article, we will learn, how we can convert a string into an enum using TypeScript. These are the two approaches that can be used to solve it: Table of Content
    5 min read
  • How to Convert String to JSON in TypeScript ?
    Converting a string to JSON is essential for working with data received from APIs, storing complex data structures, and serializing objects for transmission. Below are the approaches to converting string to JSON in TypeScript: Table of Content Convert String to JSON Using JSON.parse()Convert String
    6 min read
  • How to Convert String to Date in TypeScript ?
    In TypeScript, conversion from string to date can be done using the Date object and its method. We can use various inbuilt methods of Date object like new Date() constructor, Date.parse(), and Date.UTC. Table of Content Using new Date()Using Date.parse() Using Date.UTC()Using new Date()In this appro
    2 min read
  • How to Convert String to Number in TypeScript?
    In TypeScript, converting a string to a number is a common operation that can be accomplished using several different methods. Each method offers unique advantages and can be chosen based on the specific requirements of your application. Below are the approaches to convert string to number in TypeSc
    4 min read
  • How to Convert an Object to a JSON String in Typescript ?
    In TypeScript, an object is a collection of related data and functionality. Objects are made up of properties and methods. Properties describe the object, methods describe what it can do. Table of Content Using JSON.stringify()Using json-stringify-safe libraryUsing a Custom Serialization FunctionUsi
    5 min read
  • How to Get Value of Input in TypeScript ?
    In TypeScript, retrieving the value of an input element is a common task when building web applications. Whether you're handling form submissions, validating user input, or manipulating data based on user interactions, accessing input values is essential. The below approaches can be utilized to get
    2 min read
  • How to Convert Map to JSON in TypeScript ?
    In TypeScript, we can convert the Map to JSON by manipulating the key-value pairs of the Map into JSON-formatted string. We can use various approaches like JSON.stringify, fast-json-stringify, and json-stringify-safe Libraries for the conversion. Table of Content Using JSON.stringifyUsing fast-json-
    5 min read
  • How to Create an Enum With String Values in TypeScript ?
    To create an enum with string values in TypesScript, we have different approaches. In this article, we are going to learn how to create an enum with string values in TypesScript. Below are the approaches used to create an enum with string values in TypesScript: Table of Content Approach 1: Using Enu
    3 min read
  • How To Use @ts-ignore For A Block In TypeScript?
    The @ts-ignore is a compiler that lets the compiler ignore the line below it which is like a directive used to suppress TypeScript compiler errors upcoming on the next line of the whole code. This is mostly used when we as the developers know the code is good and useful but the compiler flags it as
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences