How to convert Boolean to String in JavaScript?


In this tutorial, we will learn to convert the Boolean to string in JavaScript. The problem is straightforward: sometimes programmers need to use the boolean true and false values as a string. So, there is a requirement to convert the Boolean to string.

Here, we have various methods to convert the Boolean to a string variable.

  • Using the toString() Method

  • Using + and $ (template literal) Operator

  • Using the ternary Operator

Using the toString() method

The toString() method is the JavaScript string library method, which is useful for converting the variables to the string data type. We can also use it for the Boolean value, and it returns a respected string value according to the Boolean value.

Syntax

Following is the syntax to convert Boolean to string using the toString() method −

let bool = false;
let result = bool.toString();

Example

In the below example, we have used the toStirng() method to convert the Boolean to string. We have converted the false value to the string and checked the type of the returned value using the typeof operators. Users can observe the results in the output.

<html> <head> </head> <body> <h2> Converting the Boolean to string in JavaScript. </h2> <h4> Converting the false to string using <i> toString() </i> method: </h4> <div id = "string1"> </div> <h4> typeof the above string is </h4> <div id = "stringType"> </div> </body> <script> var string1 = document.getElementById("string1"); var stringType = document.getElementById("stringType"); let bool = false; let result = bool.toString(); string1.innerHTML = result; stringType.innerHTML = typeof result; </script> </html>

Using + and $ (template literal) Operators

In this approach, we will use the + operator to convert the Boolean to a String. When we try to concatenate the string value with the variable of another data type, it converts the variable to the string and returns the merged string. To achieve our goal, we will concatenate the empty string with the Boolean value.

Also, users can use the template literal ($ {} ) to concatenate the empty string with the Boolean value.

Syntax

Following is the syntax to convert Boolean to string using + and $ operators −

let bool = true;
let result = bool + ""; // using + operator
let result = `${bool}`; // using template literal

Example

In the below example, we have used the + operator and template literal to convert the Boolean to string. We have simply created the formatted string of a single Boolean variable to convert the boolean into a string.

<html> <head> </head> <body> <h2> Converting the Boolean to string in JavaScript. </h2> <h4> Converting the Boolean true to string using <i> + </i> operator. </h4> <div id = "string1"> </div> <h4> Converting the Boolean false to string using <i> $ {} </i> operator. </h4> <div id = "string2"> </div> <h4> type of both returned values respectively.</h4> <div id = "stringType"> </div> </body> <script> var string1 = document.getElementById("string1"); var string2 = document.getElementById("string2"); var stringType = document.getElementById("stringType"); let bool = true; let result = bool + ""; string1.innerHTML = result; stringType.innerHTML = typeof result + "<br/>"; bool = false; result = `${bool}`; string2.innerHTML = result; stringType.innerHTML += typeof result; </script> </html>

In the above output, users can observe that the data type of both variables is the string which means we have converted Boolean to string successfully.

Using the ternary Operator

The ternary operator is the shorter version of the if-else statement. It contains three parts. The first part contains the condition. If the condition becomes true, it returns the value from the second part. Otherwise, it returns the value from the third section. We will use the Boolean variable as a condition statement and return either “true” or “false” according to the value of a Boolean variable.

Syntax

Users can follow the below syntax to use the ternary operator for Boolean values.

let bool = true;
let result = bool ? "true" : "false";

Example

In the below example, we have used the ternary operator to convert Boolean to string. The ternary operator returns the string “true” if the value of the Boolean variable is true and returns “false” if the value of the Boolean variable is false.

<html> <head> </head> <body> <h2> Converting the Boolean to string in JavaScript. </h2> <h4> Converting the Boolean true to string using <i> ternary ( ? : ) </i> operator. </h4> <div id = "string1"> </div> </body> <script> var string1 = document.getElementById("string1"); let bool = true; let result = bool ? "true" : "false"; string1.innerHTML = result; </script> </html>

We have used the tostring() method, Arithmetic + operator, and ternary operator to convert the Boolean to string. The toString() method is much slower than the second and third approaches. To make your code faster, the user should use the second approach.

Updated on: 10-Aug-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements