JavaScript Boolean valueOf() Method - GeeksforGeeks

24 Nov, 2021
Below is the example of Boolean valueOf() method.  
Example:  
javascript
<script>  // Here Boolean object obj  // is created for the value 27  var obj = new Boolean(27);   // Here boolean.valueOf() function is  // used for the created object obj.  document.write(obj.valueOf());
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:  
Program 1:  
javascript
<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
 
 
Program 2:  
javascript
<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
 
 
Program 3:  
javascript
<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
Program 4:  
javascript
<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
Program 5:  
javascript
<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
 
Program 6:  
javascript
<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
 
 
Program 7:  
javascript
<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. 
javascript
<script>  // Here Boolean object obj is created  // for the value geeksforgeeks.  var obj = new Boolean(geeksforgeeks);   // Here boolean.valueOf() function is  // used for the created object obj.  console.log(obj.valueOf());</script>
Output:  
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.  
javascript
<script>  // Here Boolean object obj is created  // for the value such as complex number 1+2i  var obj = new Boolean(1 + 2i);   // Here boolean.valueOf() function is  // used for the created object obj.  console.log(obj.valueOf());
Output:  
Error: Invalid or unexpected token
Supported Browsers: The browsers supported by JavaScript Boolean valueOf() Method are listed below: 
Google Chrome 1 and above
Internet Explorer 4 and above
Mozilla Firefox 1 and above
Safari 1 and above
Opera 4 and above
 
ysachin2314
JavaScript-Boolean
JavaScript-Methods
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Convert a string to an integer in JavaScript
How to calculate the number of days between two dates in javascript?
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
File uploading in React.js
Roadmap to Become a Web Developer in 2022
Installation of Node.js on Linux
How to fetch data from an API in ReactJS ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to insert spaces/tabs in text using HTML/CSS?