ES6 | Boolean

The Boolean of ES6 is nothing different than the other Boolean objects. It also represents two values True or False. There are two properties and three methods that build this object in ES6 JavaScript.

Properties of Boolean Object:

  • constructor: In ES6 JavaScript, the constructor property returns the constructor function for an object. For ES6 JavaScript Boolean, the constructor property returns function Boolean() { [native code] }.

    Syntax:



    boolean.constructor

    Return Value: It returns the function Boolean() { [native code] }.

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html> 
    <html
      
    <head
        <title
            JavaScript Boolean constructor Property 
        </title
    </head
      
    <body style="text-align:center;"
      
        <div style="background-color: green;"
                  
            <h1>Welcome to GeeksforGeeks!.</h1
                      
            <p
                It returns the function that created 
                the boolean's prototype: 
            </p
                      
            <h1 id="GFG"></h1
              
        </div
          
        <!-- Script to use boolean constructor -->
        <script
            var bool = false; 
            document.getElementById("GFG").innerHTML 
                    = bool.constructor; 
        </script
    </body
      
    </html>                     

    chevron_right

    
    

    Output:

  • prototype: The Boolean.prototype is an inbuilt property in ES6 JavaScript which is used to add a new property to the all Boolean instances. There is a constructor prototype that is used to add properties or methods to all Boolean objects.

    Syntax:

    Boolean.prototype.name = value

    Return Value:

    • Boolean.prototype.valueOf(): It simply returns the value of boolean object.
    • Boolean.prototype.toString(): This method returns a string according to the Boolean value.

    Example:

    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

Methods of Boolean Object:

  • valueOf() Method: The Boolean.valueOf() method is an inbuilt methods in ES6 javascript which is used to return a boolean value either “true” or “false” depending upon the value of the specified boolean object.

    Syntax:

    boolean.valueOf()

    Return Value: It returns false if the string argument is null otherwise it returns true.

    Example:


    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script
    // Here Boolean object obj is created 
    // for the value true. 
    var obj = new Boolean(true); 
      
    // Here boolean.valueOf() function is 
    // used for the created object obj. 
    document.write(obj.valueOf()); 
    </script

    chevron_right

    
    

    Output:

    true
  • toString() Method: The boolean.toString() is an inbuilt method in ES6 javascript which is used to return a string either “true” or “false” depending upon the value of the specified boolean object.

    Syntax:

    boolean.toString()

    Return Value: It returns a string either “true” or “false” depending upon the value of the specified Boolean object.

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script
    // Here Boolean object obj is 
    // created for the value true. 
    var obj = new Boolean(true); 
      
    // Here boolean.toString() function 
    // is used for the created object obj. 
    document.write(obj.toString()); 
    </script

    chevron_right

    
    

    Output:

    true
  • toSource() Method:The boolean.toSource() is an inbuilt method in ES6 JavaScript which is used to return a string representing the source code of the object.

    Syntax:

    boolean.toSource()

    Return Value: It returns a string representing the source code of the object.

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script
    // Here Boolean object obj is 
    // created for the value true. 
    var obj = new Boolean(true); 
      
    // Here boolean.toSource() function 
    // is used for the created object obj. 
    document.write(obj.toSource()); 
    </script>

    chevron_right

    
    

    Output:

    (new Boolean(true)) 

    Note: This method is not compatibale with all the browsers.



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.