Open In App

ES6 Boolean

Last Updated : 29 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The Boolean of ES6 is nothing different than the other Boolean objects. It also represents two values True or False. There are two properties and three methods that build this object in ES6 JavaScript. 

Properties of Boolean Object:

JavaScript constructor: In ES6 JavaScript, the constructor property returns the constructor function for an object. For ES6 JavaScript Boolean, the constructor property returns function Boolean() { [native code] }

Syntax:

boolean.constructor

Parameter: It does not require any parameters.

Return Value: It returns the function Boolean() { [native code] }

Example: 

html




<h1 style="color:green;">
    GeeksforGeeks
</h1>
<p>
    It returns the function that created
    the boolean's prototype:
</p>
 
<h1 id="GFG"></h1>
 
<!-- Script to use boolean constructor -->
<script>
    var bool = false;
    document.getElementById("GFG").innerHTML
            = bool.constructor;
</script>


Output: 

 

JavaScript prototype: The Boolean.prototype is an inbuilt property in ES6 JavaScript which is used to add a new property to the all Boolean instances. There is a constructor prototype that is used to add properties or methods to all Boolean objects. 

Syntax:

Boolean.prototype.name = value

Return Value:

Methods of Boolean Object:

JavaScript valueOf() Method: The Boolean.valueOf() method is an inbuilt methods in ES6 javascript which is used to return a boolean value either “true” or “false” depending upon the value of the specified boolean object. 

Syntax:

boolean.valueOf()

Return Value: It returns false if the string argument is null otherwise it returns true. 

Example: 

Javascript




// 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.
console.log(obj.valueOf());


Output:

true

JavaScript toString() Method: The boolean.toString() is an inbuilt method in ES6 javascript which is used to return a string either “true” or “false” depending upon the value of the specified boolean object. 

Syntax:

boolean.toString()

Return Value: It returns a string either “true” or “false” depending upon the value of the specified Boolean object. 

Example: 

Javascript




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


Output:

true

JavaScript toSource() Method: The boolean.toSource() is an inbuilt method in ES6 JavaScript which is used to return a string representing the source code of the object. 

Syntax:

boolean.toSource()

Return Value: It returns a string representing the source code of the object. 

Example: 

Javascript




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


Output:

(new Boolean(true)) 

Note: This method is not compatible with all browsers.

We have a complete list of Javascript ES6 features, to check those please go through this Introduction to ES6 article.

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.  



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads