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

String in ES6

String in ES6 The term string can be defined as an object used to represent the sequence of the characters. The string is widely used to store a text-based value like...

9 minutes read.

Array Methods in ES6

Array Methods in ES 6 ES 6 introduced the various new methods such as Array,from(), Array,of(). Here, we have the various array methods used in JavaScript, they are given below in the table...

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

Module in ES6

Module in ES6 In ES6, the modules can be defined as a small chunk of program code stored in a file. The modules are used to modularize the source code with...

6 minutes read.

Operators in ES6 | ECMAScript 6

Operators in ECMAScript 6 The term Operator is refers to a symbol that is used to instruct the computer system to perform a specific operation. The ECMAScript 2015 supports a rich set of operators....

11 minutes read.

Features of ES6 | ECMAScript 6

The ES6 or ECMAScript 2015 came with various exiting and new features. These features make it a more robust and updated programming language than ES5. Some of these new promising features are Const...

6 minutes read.

Immediately Invoked Function Expression ES6

Immediately Invoked Function Expression (IIFE) Immediately Invoked Function Expression (IIFE) is a JavaScript function used to ignore the variable hoisting from inside the code block. An IIFE is a function that runs...

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

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.

Map Object in ES 6

Map Object in ES 6 ES 6 introduced a new feature that is included in JavaScript. In the previous versions of ECMAScript, if we want to perform mapping on values and...

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

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.

Loop Control Statement in ES6

Loop Control Statement in ECMAScript 6 The loop control statements can be defined as a set of conditions that control the execution of the loop. It executes the loop until the condition becomes false....

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

Arrow Function in ES6

Arrow Function in ES6  An Arrow function is a new concept recommended in ECMAScript 2015. It is also called “Fat Arrow Function.” It helps us to write the function more accurately. This function...

5 minutes read.

Validation in ES6

Validation in ES6 The term validation can be defined as a mechanism to check the information given by the user is correct or not according to the requirements. If the data...

9 minutes read.

ES6 Tutorial

Introduction of ES6 The ECMAScript 6 is a scripting language specification institutionalized by ECMA International. ECMA, named in 1994, is an international organization, related to information and communication system. ECMA stands for “European Computer Manufacturers...

8 minutes read.

Class in ES6

Class in ES6: The Class is a basic term in object-oriented programming (OOPs). A class helps to determine the prototype for modeling in real-world objects. It is used to formulate...

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