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

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

Example

We have the following example to understand the above method.

Output

After the execution of the above code, we got the following output:

Boolean in ES6

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

Example

Here, we have an example for the above concept.

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

Example

Here, we have the following illustration for the same.

Output

We have the following output after the execution of the code.

Boolean in ES6

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.           PrototypeThis property enables us to associate the methods and properties to the Boolean object.
                  2.         ConstructorThe 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

Example

Here, we have an example to understand the constructor() property of the Boolean object.

Output

After the execution of the code, we got the following output:

Boolean in ES6

Return Value

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

Example

Here, we have the following example to understand the above concept.

Output

After the execution of the above example, we got the following output:

Boolean in ES6

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.

Pin It on Pinterest

Share This