Skip to content
Related Articles
Open in App
Not now

Related Articles

JavaScript hasOwnProperty() Method

Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 24 Nov, 2022
Improve Article
Save Article

The hasOwnProperty() method in JavaScript is used to check whether the object has the specified property as its own property. This is useful for checking if the object has inherited the property rather than being it’s own.

Syntax:

object.hasOwnProperty( prop )

Parameters: This method accepts a single parameter.

  • prop: It holds the name in the form of a String or a Symbol of the property to test.

Return Value: It returns a boolean value indicating whether the object has the given property as its own property.

Below examples illustrate the JavaScript hasOwnProperty() Method:

Example 1: This example checks the properties of an object.

Javascript




<p>
    Click on the button to check if
    the properties are of the object.
</p>
<p>Output for own property:
    <span class="outputProperty"></span>
</p>
<p>Output for not own property:
    <span class="outputNonProperty"></span>
</p>
<button onclick="checkProperty()">
    Check properties
</button>
<script type="text/javascript">
    function checkProperty() {
        let exampleObj = {};
        exampleObj.height = 100;
        exampleObj.width = 100;
      
        // Checking for existing property
        result1 = exampleObj.hasOwnProperty("height");
      
        // Checking for non-existing property
        result2 = exampleObj.hasOwnProperty("breadth");
      
        document.querySelector(".outputProperty").textContent
                    = result1;
          
        document.querySelector(".outputNonProperty").textContent
                    = result2;
    }
</script>

Output:

JavaScript hasOwnProperty() Method

JavaScript hasOwnProperty() Method

Example 2: This example checks the properties of an object of a class.

html




<p>
    Click on the button to check if the
    properties are of the object.
</p>
<p>Output for own property:
    <span class="outputProperty"></span>
</p>
<p>Output for not own property:
    <span class="outputNonProperty"></span>
</p>
<button onclick="checkProperty()">
    Check properties
</button>
  
<script type="text/javascript">
    function checkProperty() {
          
        function Car(a, b) {
            this.model = a;
            this.name = b;
        }
      
        let car1 = new Car("Mazda", "Laputa");
      
        // Checking for existing property
        result1 = car1.hasOwnProperty("model");
      
        // Checking for non-existing property
        result2 = car1.hasOwnProperty("wheels");
      
        document.querySelector(".outputProperty").textContent
                    = result1;
          
        document.querySelector(".outputNonProperty").textContent
                    = result2;
    }
</script>

Output:

JavaScript hasOwnProperty() Method

JavaScript hasOwnProperty() Method

We have a complete list of Object methods, and properties to check those please go through this JavaScript Object Complete Reference article.

Supported Browsers: The browsers supported by JavaScript hasOwnProperty() method are listed below.

  • Google Chrome 1 and above
  • Firefox 1 and above
  • Internet Explorer 5.5 and above
  • Edge 12 and above
  • Safari 3 and above
  • Opera 5 and above

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!