Skip to content
Related Articles

Related Articles

Improve Article

ES6 | Boolean

  • Last Updated : 11 Oct, 2019

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:






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

    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:




    <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

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:






    <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

    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:




    <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

    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:




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

    Output:

    (new Boolean(true)) 

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




My Personal Notes arrow_drop_up
Recommended Articles
Page :