;

How to Convert a Boolean to a String in Javascript


Tutorialsrack 03/04/2022 Jquery Javascript

In this article, you’ll learn how to convert a boolean to a string in javascript. There are two ways to convert a boolean value to a string value in JavaScript.

Here is the example to convert a boolean to a string in Javascript.

Example 1: Using Boolean.toString() method

In this example, we used the toString() method to convert a boolean (true or false) to its respective string.

console.log(true.toString()); 
// output ==> "true"

console.log(false.toString()); 
// output ==> "false"

Example 2: Using String Concatenation

In this example, you can concatenate a boolean value and an empty string ('') to convert it to a string.

console.log(''+true);
// output ==> "true"

console.log(typeof (''+false) === 'string');
// it checks if the value is string or not 
// output ==> true

I hope this article will help you to understand how to convert a boolean to a string in javascript.

Share your valuable feedback, please post your comment at the bottom of this article. Thank you!


Related Posts



Comments

Recent Posts
Tags