JavaScript Boolean toString()

Posted by

Method

JavaScript Boolean toString() Method

The JavaScript Boolean.prototype.toString() method is used to convert a Boolean object to a string. It is a function that is part of the JavaScript Boolean prototype and is used to convert a boolean to a string.

A Boolean is a data type that has two possible values, either true or false. The Boolean.prototype.toString() method lets us convert a given Boolean object to a string, either “true” or “false”.

Syntax

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

boolean.toString()

[/dm_code_snippet]

Parameters

The Boolean.prototype.toString() method does not take any parameters.

Return Value

The Boolean.prototype.toString() method returns a string, either “true” or “false”, depending on the value of the Boolean object.

Examples

The following example shows how to use the Boolean.prototype.toString() method:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

// Create a Boolean object
let myBoolean = new Boolean(true);

// Convert the Boolean object to a string
let myString = myBoolean.toString();

// Output the string
console.log(myString); // Outputs "true"

[/dm_code_snippet]

In this example, we create a Boolean object and assign it the value of true. We then use the Boolean.prototype.toString() method to convert it to a string and then output the string. The output of this code will be “true”.

Browser Support

The Boolean.prototype.toString() method is supported in all modern browsers, including Chrome, Firefox, Edge, and Safari.