ES6 Boolean

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.

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

Boolean Properties

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.

ES6 Boolean constructor()

The JavaScript Boolean constructor() method is used to return the reference to the Boolean function that created the Boolean prototype.

Syntax
snippet
Boolean.constructor

Return value

Boolean() { [native code] }.
Example
snippet
var example = new Boolean( ); 
console.log("example.constructor() is : " + example.constructor);
Output
example.constructor() is : function Boolean() { [native code] }
ES6 Boolean prototype

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.

Syntax
Boolean.prototype.name = value

Return value

  • Boolean.prototype.valueOf(): It is used to return the value of Boolean object.
  • Boolean.prototype.toString(): It is used to return a string according to the Boolean value.
Example
snippet
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();
Output
Yellow

Boolean Methods

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.

Boolean.prototype.toSource()

This method returns a string that contains the source code of the Boolean object. It overrides the Object.prototype.toSource() method.

Note
This method is not compatible with all browsers.
Syntax
boolean.toSource();
Example
snippet
<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.

Boolean.prototype.toString()

It returns a string of either true or false depends on the Boolean object value.

Syntax
Boolean.toString()
Example
snippet
var obj = new Boolean(true);  
console.log(obj.toString());
Output
true
Boolean.prototype.valueOf()

It returns the primitive value of the Boolean object.

Syntax
boolean.valueOf()
Example
snippet
var obj = new Boolean(true);  
console.log(obj.valueOf());
Output
true
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +