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.
var val = new Boolean(value);
There are three methods and two properties of the Boolean object. Let us try to understand the properties and methods of the Boolean object.
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.
The JavaScript Boolean constructor() method is used to return the reference to the Boolean function that created the Boolean prototype.
Boolean.constructor
Return value
Boolean() { [native code] }.
var example = new Boolean( ); console.log("example.constructor() is : " + example.constructor);
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.
Boolean.prototype.name = value
Return value
Boolean.prototype.color = function() { if (this.valueOf() == true) { return "Yellow"; } else { return "Orange"; } }; function show() { var my_color = true; console.log(my_color.color()); } show();
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.
This method returns a string that contains the source code of the Boolean object. It overrides the Object.prototype.toSource() method.
boolean.toSource();
<script> var obj = new Boolean(true); document.write(obj.toSource()); </script>
You can run the above example in the Firefox browser because this method is not compatible with other browsers.
It returns a string of either true or false depends on the Boolean object value.
Boolean.toString()
var obj = new Boolean(true); console.log(obj.toString());
It returns the primitive value of the Boolean object.
boolean.valueOf()
var obj = new Boolean(true); console.log(obj.valueOf());