Skip to content
Related Articles
Open in App
Not now

Related Articles

JavaScript Boolean prototype Constructor

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 02 Jan, 2023
Improve Article
Save Article

The Boolean prototype Constructor property is used to add a new property and methods to all Boolean instances. By the time of creating properties ALL booleans will be given the property, and it’s valued, as default but in case of methods, ALL booleans will have this method available. 

Syntax:

Boolean.prototype.name = value

Here “name” specifies the property or method name to be used and “value” specifies the value used by the function.

 Some methods related to Boolean.prototype property:

Below are examples of the Boolean prototype Constructor property.

Example 1: 

javascript




Boolean.prototype.color = function(pro){
    if (pro == true) {
    return "Green";
    } else {
    return "Orange";
    }
};
  
// Creating a new boolean object
var Obj = new Boolean(true);
console.log("Color of Gfg = "
                + Obj.color(true));

Output:

Color of Gfg = Green

Example 2: 

javascript




function check(v1) {
    if (v1 == true)
    return (v1 + " is True.");
    else
    return (v1 + " is False.");
}
  
// Adding a new property
Boolean.prototype.myVar = false;
  
// Adding a new method
Boolean.prototype.myMethod = check;
  
// Creating a new boolean object
var Obj1 = new Boolean();
  
console.log(Obj1.myMethod(1) + "<br>");
console.log(Obj1.myMethod(0) + "<br>");
console.log("myVar = " + Obj1.myVar);

Output:

1 is True.
0 is False.
myVar = false

Example 3: 

javascript




Boolean.prototype.CheckGeek = function(pro) {
    if (pro == true) {
        return "Pro Geek";
    } else {
        return "Geek";
    }
};
  
// Creating a new boolean object
var Obj = new Boolean(true);
  
console.log("Obj.CheckGeek(true) = "
             + Obj.CheckGeek(true) + "<br>");
console.log("Obj.CheckGeek(false) = "
             + Obj.CheckGeek(false) + "<br>");
console.log("Obj.valueOf() = "
             + Obj.valueOf() + "<br>");
console.log("Obj.toString() = "
             + Obj.toString());

Output:

Obj.CheckGeek(true) = Pro Geek
Obj.CheckGeek(false) = Geek
Obj.valueOf() = true
Obj.toString() = true

Supported Browsers: The browsers supported by JavaScript Boolean prototype constructor are listed below:

  • Google Chrome
  • Internet Explorer
  • Mozilla Firefox
  • Safari
  • Opera

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!