The boolean.toString() method is used to return a string either “true” or “false” depending upon the value of the specified boolean object.
Syntax:
boolean.toString()
Parameter: This method does not accept any parameter.
Return Values: It returns a string either “true” or “false” depending upon the value of the specified boolean object.
Below are examples of the Boolean toString() method.
Example 1:
javascript
// Here Boolean object obj // is created for the value 27 let obj = new Boolean(27); // Here boolean.toString() function is // used for the created object obj. console.log(obj.toString()); |
Output:
true
Example 2:
javascript
// Here Boolean object obj is // created for the value true. let obj = new Boolean( true ); // Here boolean.toString() function // is used for the created object obj. console.log(obj.toString()); |
Output:
true
Example 3:
javascript
// Here Boolean object obj is // created for the value 1. let obj = new Boolean(1); // Here boolean.toString() function is // used for the created object obj. console.log(obj.toString()); |
Output:
true
Example 4:
javascript
// Here Boolean object obj is // created for the value -1. let obj = new Boolean(-1); // Here boolean.toString() function is // used for the created object obj. console.log(obj.toString()); |
Output:
true
Example 5:
javascript
// Here Boolean object obj is // created for the value 1.2 let obj = new Boolean(1.2); // Here boolean.toString() function is // used for the created object obj. console.log(obj.toString()); |
Output:
true
Example 6:
javascript
// Here Boolean object obj is created // for the value as string "gfg" let obj = new Boolean( "gfg" ); // Here boolean.toString() function is // used for the created object obj. console.log(obj.toString()); |
Output:
true
Example 7:
javascript
// Here Boolean object obj is created // for the value false. let obj = new Boolean( false ); // Here boolean.toString() function is // used for the created object obj. console.log(obj.toString()); |
Output:
false
Errors and Exceptions: If we try to assign a new boolean with a variable that is not defined or a complex number then an error will be thrown instead of the default true/false value.
Example 1: Here the value as neveropen gives an error because this value is not defined only true and false have been predefined.
javascript
// Here Boolean object obj is created // for the value neveropen. let obj = new Boolean(neveropen); // Here boolean.toString() function is // used for the created object obj. console.log(obj.toString()); |
Output:
Error: neveropen is not defined
Example 2: Here complex number can not be taken as the parameter only integer values and strings can be taken as the parameter which is why it returns an error.
javascript
// Here Boolean object obj is created for the // value such as complex number 1+2i let obj = new Boolean(1 + 2i); // Here boolean.toString() function is // used for the created object obj. console.log(obj.toString()); |
Output:
Error: Invalid or unexpected token
We have a complete list of Javascript Date Objects, to check those please go through this JavaScript Boolean Complete Reference article.
Supported Browsers: The browsers supported by JavaScript Boolean toString() Method are listed below:
- Google Chrome
- Internet Explorer
- Mozilla Firefox
- Opera
- Safari
We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.