JavaScript Boolean toString() Method
Last Updated :
24 May, 2023
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
let obj = new Boolean(27);
console.log(obj.toString());
|
Output:
true
Example 2:
javascript
let obj = new Boolean( true );
console.log(obj.toString());
|
Output:
true
Example 3:
javascript
let obj = new Boolean(1);
console.log(obj.toString());
|
Output:
true
Example 4:
javascript
let obj = new Boolean(-1);
console.log(obj.toString());
|
Output:
true
Example 5:
javascript
let obj = new Boolean(1.2);
console.log(obj.toString());
|
Output:
true
Example 6:
javascript
let obj = new Boolean( "gfg" );
console.log(obj.toString());
|
Output:
true
Example 7:
javascript
let obj = new Boolean( false );
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 geeksforgeeks gives an error because this value is not defined only true and false have been predefined.
javascript
let obj = new Boolean(geeksforgeeks);
console.log(obj.toString());
|
Output:
Error: geeksforgeeks 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
let obj = new Boolean(1 + 2i);
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.