ES6 Boolean
The ES6 Boolean objects can represent two values, either ‘true’ or ‘false’. In JavaScript, the Boolean is used as a function to get the value of an object, a variable, conditions, expressions, and many more in terms of true and false.
The object has the initial false value if the value parameter is omitted or 0, negative, false, null, NaN, undefined, or an empty (“”) string.
Syntax
There are three methods and two properties of the Boolean object. Let us try to understand the properties and methods of the Boolean object.
Boolean Properties
There are two properties of the Boolean object that are tabulated as follows:
S.no. | Properties | Description |
---|---|---|
1. | Constructor | This property returns a constructor function for an object. |
2. | Prototype | It is used to add properties and methods to the Boolean instances. |
Let us try to elaborate on the above Boolean properties.
ES6 Boolean constructor() property
The JavaScript Boolean constructor() method is used to return the reference to the Boolean function that created the Boolean prototype.
Syntax
Return value
Example
Output
example.constructor() is : function Boolean() { [native code] }
ES6 Boolean prototype property
It is an inbuilt property in ES6, which is used for adding new properties and methods to any Boolean instance such as Number, String, Date, etc. It is a global property which is available with almost all objects.
Syntax
Return value
- Boolean.prototype.valueOf(): It is used to return the value of Boolean object.
- Boolean.prototype.toString(): It is used to return a string according to the Boolean value.
Example
Output
Yellow
Boolean Methods
The Boolean object contains three methods, which are tabulated as follows:
S.no. | Methods | Description |
---|---|---|
1. | toSource() | This method returns a string having a source of the Boolean object. |
2. | toString() | It returns a string of either true or false depends on the Boolean object value. |
3. | valueOf() | It returns the primitive value of the Boolean object. |
Let us try to elaborate on the above Boolean methods.
Boolean.prototype.toSource() method
This method returns a string that contains the source code of the Boolean object. It overrides the Object.prototype.toSource() method.
Note: This method is not compatible with all browsers.
Syntax
Example
You can run the above example in the Firefox browser because this method is not compatible with other browsers.
Boolean.prototype.toString() method
It returns a string of either true or false depends on the Boolean object value.
Syntax
Example
Output
true
Boolean.prototype.valueOf() method
It returns the primitive value of the Boolean object.
Syntax
Example
Output
true