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 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

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.

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

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:

Boolean in ES6

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:

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.


Related Topics

Generator in ES6 | ECMAScript 6

ECMAScript 6 recommends a new way of working with iterators and functions. The way is named as generator or generator function. The ES 6 generator is a distinct type of function that...

5 minutes read.

Array in ES6

Array in ES6 The array can be defined as an object that is used to display the set of homogenous type of elements in ECMAScript 6. The array provides the facility to store...

5 minutes read.

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...

4 minutes read.

Object Destructuring in ES6

Object Destructuring in ES6 The term destructuring is referred to as the breakdown of the complex entities into simpler parts. It works like array destructuring. In the array destructuring, the values...

3 minutes read.

Loops in ES6 | ECMAScript 6

Loops in ECMAScript 6 A Loop statement can be defined as a group of instructions that are always repeated. In the computer programming language, the loops help us to execute a collection of instructions...

8 minutes read.

Environment Set up in ES6

Environment Set up in ES6 JavaScript is a portable language. It can execute on any browser or any host, and any operating system. Before the use of JavaScript, we need to...

5 minutes read.

Animation in ES6

Animation in ES6 In JavaScript, the animation feature is used to manage the aspect that the CSS (Cascading Style Sheet) can’t. JavaScript provides various new elements that help us to construct...

8 minutes read.

Spread Operator in ES6

Spread Operator in ECMAScript 6 The term 'Spread Operator' can be defined as an operator that allows expending the array into separate elements. Three dots (…) isthe syntax of the Spread Operator.The Spread operator...

5 minutes read.

Functions in ES6 | ECMAScript 6

Functions in ECMAScript 6 In ECMAScript programming language, the term ‘function’ can be defined as a collection or group of input statements, used to perform particular computational calculations and generate the output...

9 minutes read.

Promises in ES6

Promises in ES6 A promise is used to represent some aspects that are finally fulfilled. It can also be resolved or discarded according to the operational output. In ES6, a promise...

8 minutes read.

Events in ES6

Events in ES6 The events can be defined as the actions or the occurrence of an action that is perceived by the software. The event is used to handle the interaction between...

14 minutes read.

Decision-Making in ES6 | ECMAScript 6

Decision-Making in ECMAScript 6 The Decision-making structure or statements are used to determine one or more than one condition to be evaluated or tested. The Decision-making statement helps us to decide...

7 minutes read.

Template Literals in ES6

Template Literals in ES6            ES6 introduced a new feature called template literals. It facilitates us to define a multiline string and executes the string interpolation. The template literals are referred to...

3 minutes read.

Cookies in ES6

Cookies in ES6 The cookies are an old client-side storage technique produced in a server-side scripting language such as Php and ASP etc. The cookie can be defined as a small...

6 minutes read.

Set in ES6

Set in ES6 A Set is a kind of object used to create a group of unique values. The values stored in a set can be string, literal, array, and integer....

6 minutes read.

Types of Inheritance in ES6

Types of Inheritance The term inheritance can be divided into following three parts- Single-level InheritanceMultiple InheritanceMulti-level Inheritance Single-level Inheritance The single-level inheritance can be defined as an inheritance in which the child class can...

5 minutes read.

Page Redirect in ES6

Page Redirect in ES6 The term redirect can be defined as a process to send the user and search engine on a specified URL from the original page. “The page redirection is...

3 minutes read.

Object in ES6

Object in ES6 The term object can be defined as an instance that contains the group of key-value pairs. We can modify it using the life cycle of an object in...

6 minutes read.

Number in ES6

Number in ES6 In ES6, we have various methods and properties to implement numeric functions such as floating-point, integers, date, etc. The numbers in ES6 enable us to work with number...

7 minutes read.

Dialog box in ES6

Dialog box in ES6 A dialog box can be defined as a regular type of window in the Graphical User Interface (GUI) of the operating system. It is also spelled as...

5 minutes read.