Below is the example of Boolean valueOf() method.
- Example:
<script>// Here Boolean object obj// is created for the value 27varobj =newBoolean(27);// Here boolean.valueOf() function is// used for the created object obj.document.write(obj.valueOf());chevron_rightfilter_none - Output:
true
The boolean.valueOf() method is used to return a boolean value either “true” or “false” depending upon the value of the specified boolean object.
Syntax:
boolean.valueOf()
Parameter: This method does not accept any parameter.
Return value: It returns a boolean value either “true” or “false” depending upon the value of the specified boolean object.
More codes for the above method are as follows:
<script> // Here Boolean object obj is created // for the value true. var obj = new Boolean(true); // Here boolean.valueOf() function is // used for the created object obj. document.write(obj.valueOf()); </script> |
Output:
true
<script> // Here Boolean object obj is // created for the value 1. var obj = new Boolean(1); // Here boolean.valueOf() function // is used for the created object obj. document.write(obj.valueOf()); </script> |
Output:
true
<script> // Here Boolean object obj is // created for the value -1. var obj = new Boolean(-1); // Here boolean.valueOf() function // is used for the created object obj. document.write(obj.valueOf()); </script> |
Output:
true
<script> // Here Boolean object obj is // created for the value 1.2 var obj = new Boolean(1.2); // Here boolean.valueOf() function // is used for the created object obj. document.write(obj.valueOf()); </script> |
Output:
true
<script> // Here Boolean object obj is // created for the value as string "gfg" var obj = new Boolean("gfg"); // Here boolean.valueOf() function is // used for the created object obj. document.write(obj.valueOf()); </script> |
Output:
true
<script> // Here Boolean object obj is created for the value false. var obj = new Boolean(false); // Here boolean.valueOf() function is // used for the created object obj. document.write(obj.valueOf()); </script> |
Output:
false
<script> // Here Boolean object obj is created // for the value zero (0) var obj = new Boolean(0); // Here boolean.valueOf() function is // used for the created object obj. document.write(obj.valueOf()); </script> |
Output:
false
- Program 1: Here the value as geeksforgeeks gives an 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.valueOf() function is// used for the created object obj.console.log(obj.valueOf());</script>chevron_rightfilter_noneOutput:
Error: geeksforgeeks is not defined
- Program 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.valueOf() function is// used for the created object obj.console.log(obj.valueOf());chevron_rightfilter_noneOutput:
Error: Invalid or unexpected token
Errors and Exceptions: Check the console for this Programs.
Supported Browsers: The browsers supported by JavaScript Boolean valueOf() Method are listed below:
Recommended Posts:
- JavaScript Number valueOf( ) Method
- JavaScript Date valueOf() Method
- JavaScript Array valueOf() Method
- JavaScript string.valueOf() Method
- JavaScript | BigInt.prototype.valueOf() Method
- JavaScript | symbol.valueOf() function
- TypeScript | String valueOf() Method
- TypeScript Number valueOf() Method
- Lodash _.prototype.valueOf() Method
- Moment.js moment().valueOf() Function
- JavaScript Boolean toString() Method
- JavaScript Boolean
- JavaScript Boolean prototype Constructor
- Sort an array of objects using Boolean property in JavaScript
- JavaScript Boolean constructor Property
- JavaScript | Convert a string to boolean
- Convert boolean result into number/integer in JavaScript
- JavaScript | Boolean and dataView Complete Reference
- How to generate a random boolean using JavaScript ?
- How to toggle a boolean using JavaScript ?
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.


