Skip to content
Related Articles

Related Articles

JavaScript Boolean prototype Constructor
  • Difficulty Level : Medium
  • Last Updated : 27 Jun, 2020

Below is the example of the Boolean prototype Constructor property.

  • Example:




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

    
    

  • Output:
    Color of Gfg = Green

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:

More example codes for the above constructor are as follows:
Program 1:






<script>
        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();
  
        document.write(Obj1.myMethod(1) + "<br>");
        document.write(Obj1.myMethod(0) + "<br>");
        document.write("myVar = " + Obj1.myVar);
      
</script>


Output:

1 is True.
0 is False.
myVar = false

Program 2:




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


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

full-stack-img

My Personal Notes arrow_drop_up
Recommended Articles
Page :