JavaScript | boolean.toString() function
The boolean.toString() is an inbuilt function in javascript which is used to return a string either “true” or “false” depending upon the value of the specified boolean object.
Syntax:
boolean.toString()
Parameter:
Here nothing as parameter is passed.
Return Values:
It returns a string either “true” or “false” depending upon the value of the specified boolean object.
Let’s see some JavaScript code on this boolean.toString() function:
- Code #1:
// Here Boolean object obj is// created for the value true.varobj =newBoolean(true);// Here boolean.toString() function// is used for the created object obj.console.log(obj.toString());chevron_rightfilter_noneOutput:
> "true"
- Code #2:
// Here Boolean object obj is// created for the value 1.varobj =newBoolean(1);// Here boolean.toString() function is// used for the created object obj.console.log(obj.toString());chevron_rightfilter_noneOutput:
> "true"
- Code #3:
// Here Boolean object obj is// created for the value -1.varobj =newBoolean(-1);// Here boolean.toString() function is// used for the created object obj.console.log(obj.toString());chevron_rightfilter_noneOutput:
> "true"
- Code #4:
// Here Boolean object obj is// created for the value 1.2varobj =newBoolean(1.2);// Here boolean.toString() function is// used for the created object obj.console.log(obj.toString());chevron_rightfilter_noneOutput:
> "true"
- Code #5:
// Here Boolean object obj is created// for the value as string "gfg"varobj =newBoolean("gfg");// Here boolean.toString() function is// used for the created object obj.console.log(obj.toString());chevron_rightfilter_noneOutput:
> "true"
- Code #6:
// Here Boolean object obj is created// for the value false.varobj =newBoolean(false);// Here boolean.toString() function is// used for the created object obj.console.log(obj.toString());chevron_rightfilter_noneOutput:
> "false"
- Here the value as geeksforgeeks gives error because this value is not defined only true and false has been predefined.
// Here Boolean object obj is created// for the value geeksforgeeks.varobj =newBoolean(geeksforgeeks);// Here boolean.toString() function is// used for the created object obj.console.log(obj.toString());chevron_rightfilter_noneOutput:
Error: geeksforgeeks is not defined
- Here complex number can not be taken as the parameter only integer values and string can be taken as the parameter that is why it returns error.
// Here Boolean object obj is created for the// value such as complex number 1+2ivarobj =newBoolean(1 + 2i);// Here boolean.toString() function is// used for the created object obj.console.log(obj.toString());chevron_rightfilter_noneOutput:
Error: Invalid or unexpected token
Errors and Exceptions:
- This function’s application is to return a string of either “true” or “false” depending upon the value of the object given.
Let’s see JavaScript program on this application:// Here Boolean object obj// is created for the value 27varobj =newBoolean(27);// Here boolean.toString() function is// used for the created object obj.console.log(obj.toString());chevron_rightfilter_noneOutput:
> "true"
Application:
Recommended Posts:
- Javascript | Number() Function
- JavaScript | Function Apply
- JavaScript | Array some() function
- JavaScript | Function Invocation
- JavaScript | toExponential() Function
- JavaScript | Math.pow( ) Function
- JavaScript | String() Function
- JavaScript | Symbol.for() function
- JavaScript | Function Parameters
- JavaScript | Function Definitions
- JavaScript | Array every() function
- JavaScript | Array.of() function
- JavaScript | Math.E() function
- JavaScript | toPrecision( ) Function
- JavaScript | toString( ) function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




