Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

JavaScript | hasOwnProperty() Method

  • Difficulty Level : Basic
  • Last Updated : 22 Nov, 2021

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 single parameter prop which 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. 
Example 1: This example checks the properties on an object.
 

html




<!DOCTYPE html>
<html>
 
<head>
    <title>
        JavaScript | hasOwnProperty() Method
    </title>
</head>
 
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
 
    <b>
        hasOwnProperty() method in JavaScript
    </b>
     
     
 
<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>
</body>
 
</html>

Output: 
 

  • Before clicking the button: 
     

object-before

  • After clicking the button: 
     

object-after



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

html




<!DOCTYPE html>
<html>
 
<head>
    <title>
        JavaScript hasOwnProperty() method
    </title>
</head>
 
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
     
    <b>
        hasOwnProperty() method in JavaScript
    </b>
     
     
 
<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>
</body>
 
</html>

Output: 
 

  • Before clicking the button: 
     

class-object-before

  • After clicking the button: 
     

class-object-after

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
Recommended Articles
Page :

Start Your Coding Journey Now!