JavaScript Boolean prototype Constructor
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:
- Boolean.prototype.valueOf(): It simply returns the value of boolean object.
- Boolean.prototype.toString(): This method returns a string according to the Boolean value.
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.
Please Login to comment...