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.
Similar Reads
Dart - Boolean Dart language provides a pre-defined data type called boolean which can store two possible values, either true or false. To declare a boolean variable in Dart programming language, the keyword bool is used. Most commonly, boolean is used in decision-making statements. The syntax for declaring a bool
2 min read
Perl | Boolean Values In most of the programming language True and False are considered as the boolean values. But Perl does not provide the type boolean for True and False. In general, a programmer can use the term "boolean" when a function returns either True or False. Like conditional statements(if, while, etc.) will
3 min read
Python Boolean Python Boolean type is one of the built-in data types provided by Python, which represents one of the two values i.e. True or False. Generally, it is used to represent the truth values of the expressions.Python Boolean TypeBoolean value can be of two types only i.e. either True or False. The output
7 min read
Boolean Data Type In programming languages, we have various data types to store different types of data. Some of the most used data types are integer, string, float, and boolean. The boolean data type is a type of data that stores only two types of values i.e. True or False. These values are not case-sensitive depend
13 min read
Boolean Functions Boolean Algebra was given by George Boole. It is a set of rules used to simplify a given logical expression without changing its functionality. It is mainly used when several variables present are less. The algebraic expression used in Boolean Algebra is known as Boolean Expression and it is used to
4 min read
JavaScript Boolean To represent logical values, JavaScript uses the Boolean data type, which has two possible values: true or false. These values often result from comparisons or logical operations. Additionally, the Boolean() function can convert other types of values into Boolean, determining their truthy or falsy n
4 min read