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: This function does not accepts any single parameter.
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:
- Example 1:
<script>// Here Boolean object obj is// created for the value true.varobj =newBoolean(true);// Here boolean.toString() function// is used for the created object obj.document.write(obj.toString());</script>chevron_rightfilter_noneOutput:
true
- Example 2:
<script>// Here Boolean object obj is// created for the value 1.varobj =newBoolean(1);// Here boolean.toString() function is// used for the created object obj.document.write(obj.toString());</script>chevron_rightfilter_noneOutput:
true
- Example 3:
<script>// Here Boolean object obj is// created for the value -1.varobj =newBoolean(-1);// Here boolean.toString() function is// used for the created object obj.document.write(obj.toString());</script>chevron_rightfilter_noneOutput:
true
- Example 4:
<script>// 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.document.write(obj.toString());</script>chevron_rightfilter_noneOutput:
true
- Example 5:
<script>// 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.document.write(obj.toString());</script>chevron_rightfilter_noneOutput:
true
- Example 6:
<script>// Here Boolean object obj is created// for the value false.varobj =newBoolean(false);// Here boolean.toString() function is// used for the created object obj.document.write(obj.toString());</script>chevron_rightfilter_noneOutput:
false
Errors and Exceptions:
- Example 1: Here the value as geeksforgeeks gives error because this value is not defined only true and false has been predefined.
<script>// 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());</script>chevron_rightfilter_noneOutput:
Error: geeksforgeeks is not defined
- Example 2: 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.
<script>// 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());</script>chevron_rightfilter_noneOutput:
Error: Invalid or unexpected token
Application:
- 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:
Example:<script>// Here Boolean object obj// is created for the value 27varobj =newBoolean(27);// Here boolean.toString() function is// used for the created object obj.document.write(obj.toString());</script>chevron_rightfilter_noneOutput:
true
Recommended Posts:
- How to call a function that return another function in JavaScript ?
- JavaScript | Function Definitions
- JavaScript | Function Parameters
- JavaScript | toExponential() Function
- How to override a JavaScript function ?
- JavaScript | Function binding
- JavaScript | Math.abs( ) function
- JavaScript | toFixed( ) Function
- JavaScript | String() Function
- JavaScript | Function Call
- JavaScript | Function Apply
- JavaScript | Array some() function
- JavaScript | Array.of() function
- Javascript | Number() Function
- JavaScript | Array every() 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.


