Open In App
Related Articles

JavaScript Boolean valueOf() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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.

Example 1: Below is an example of the Boolean valueOf() method. 

javascript

// Here Boolean object obj
// is created for the value 27
let obj = new Boolean(27);
 
// Here boolean.valueOf() function is
// used for the created object obj.
console.log(obj.valueOf());

                    

Output: 

true

Example 2: Here the Boolean object is created with the value true and used with the boolean.valueOf() method.

javascript

// Here Boolean object obj is created
// for the value true.
let obj = new Boolean(true);
 
// Here boolean.valueOf() function is
// used for the created object obj.
console.log(obj.valueOf());

                    

Output: 

true

Example 3: Here the Boolean object is created with the value 1 and used with the boolean.valueOf() method.

javascript

// Here Boolean object obj is
// created for the value 1.
let obj = new Boolean(1);
 
// Here boolean.valueOf() function
// is used for the created object obj.
console.log(obj.valueOf());

                    

Output: 

true

Example 4: Here the Boolean object is created with the value -1 and used with the boolean.valueOf() method.

javascript

// Here Boolean object obj is
// created for the value -1.
let obj = new Boolean(-1);
 
// Here boolean.valueOf() function
// is used for the created object obj.
console.log(obj.valueOf());

                    

Output: 

true

Example 5: Here the Boolean object is created with the value 1.2 and used with the boolean.valueOf() method.

javascript

// Here Boolean object obj is
// created for the value 1.2
let obj = new Boolean(1.2);
 
// Here boolean.valueOf() function
// is used for the created object obj.
console.log(obj.valueOf());

                    

Output: 

true

Example 6: Here the Boolean object is created with the string value gfg and used with the boolean.valueOf() method.

javascript

// Here Boolean object obj is
// created for the value as string "gfg"
let obj = new Boolean("gfg");
 
// Here boolean.valueOf() function is
// used for the created object obj.
console.log(obj.valueOf());

                    


Output: 

true

Example 7: Here the Boolean object is created with the value false and used with the boolean.valueOf() method.

javascript

// Here Boolean object obj is created for the value false.
let obj = new Boolean(false);
 
// Here boolean.valueOf() function is
// used for the created object obj.
console.log(obj.valueOf());

                    

Output: 

false

Example 8: Here the Boolean object is created with the value 0 and used with the boolean.valueOf() method.

javascript

// Here Boolean object obj is created
// for the value zero (0)
let obj = new Boolean(0);
 
// Here boolean.valueOf() function is
// used for the created object obj.
console.log(obj.valueOf());

                    

Output: 

false

Program 9: Here the value as geeksforgeeks gives an error because this value is not defined only true and false have been predefined.

javascript

// Here Boolean object obj is created
// for the value geeksforgeeks.
let obj = new Boolean(geeksforgeeks);
 
// Here boolean.valueOf() function is
// used for the created object obj.
console.log(obj.valueOf());

                    

Output: 

Error: geeksforgeeks is not defined

Program 10: Here complex numbers 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

// Here Boolean object obj is created
// for the value such as complex number 1+2i
let 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

We have a complete list of Javascript Boolean methods, to check those please go through the Javascript Boolean Complete Reference article.

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


Last Updated : 19 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads