JavaScript Boolean prototype Constructor

Below is the example of the Boolean prototype Constructor property.

  • Example:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

  • 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:



filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

1 is True.
0 is False.
myVar = false

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.