Boolean in ES6
Boolean in ES6
In ES6, the Boolean object is used to show two values either it may be true or false. In JavaScript, we can use the Boolean object as a function. It is used to get the value of an object, expression, condition, variables, and various aspects of true and false. The basic value of the Boolean object is false if we consider the value parameter is 0 or discarded, false, null, NaN, negative, undefined, or an empty string (“”).
Syntax
var value = new Boolean(value);
Methods of Boolean Object
There are following three methods of Boolean object introduced in ES6. They are listed below in the tabular form.
Sr. No. | Method | Description |
1. | valueOf() | This method gives the value (primitive) of the Boolean object. |
2. | toSource() | It returns a string value, which consist of the source of the Boolean object. |
3. | toString() | It is used to return string of true or false according to the Boolean object value. |
Here, we have tried to elaborate each method in detail with an example.
Boolean.prototype.valueOf() method
The Boolean.prototype.valueOf() method returns the primitive value of Boolean object.
Syntax
Boolean.valueOf()
Example
We have the following example to understand the above method.
var object = new Boolean(false); console.log(object.valueOf());
Output
After the execution of the above code, we got the following output:
Boolean.prototype.toSource() method
This method returns a string that consists of the source code of the Boolean object. This method is not supportable for all browsers.
Syntax
Boolean.toSource();
Example
Here, we have an example for the above concept.
<script> var object = new Boolean(false); document.write(object.toSource()); </ script >
We can run the above code on Mozilla browser because it is not supportable for all browsers.
Boolean.prototype.toString() method
It gives us a string that holds true or false value according to Boolean object value.
Syntax
Boolean.toString()
Example
Here, we have the following illustration for the same.
var object = new Boolean(false); console.log(object.toString());
Output
We have the following output after the execution of the code.
Properties of Boolean Object
There are two properties of a Boolean object which are listed below in the tabular form.
Sr. No. | Property | Description |
1. | Prototype | This property enables us to associate the methods and properties to the Boolean object. |
2. | Constructor | The Constructor property gives the constructor function, which is used for an object. |
Here, we have a detailed description of the above properties with an example.
Constructor() Property
In JavaScript, the Boolean constructor() is used to give us the constructor() function that construct a Boolean prototype.
Syntax
Boolean.constructor
Example
Here, we have an example to understand the constructor() property of the Boolean object.
var instance = new Boolean( ); console.log(“instance.constructor is : ” + instance.constructor);
Output
After the execution of the code, we got the following output:
Return Value
Boolean() { [native code] }
Prototype () Property
In ES6, it is a default property that is used to associate the methods and properties to any Boolean object such as String, Date, and Number. The prototype() property is referred as globally used property. We can use it with almost all the objects.
Syntax
Boolean.prototype.name = value
Example
Here, we have the following example to understand the above concept.
Boolean.prototype.animal = function() { if(this.valueOf() = = true) { return “Cat”; } else { return “Horse”; } }; function display() { var my_animal = true; console.log(my_animal.animal()); } display();
Output
After the execution of the above example, we got the following output:
Return Value
Boolean.prototype.toString(): It returns a string value that depends on the Boolean value.
Boolean.prototype.valueOf(): It returns the Boolean object value.