JavaScript | hasOwnProperty() Method

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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
  • Firefox 1
  • Internet Explorer
  • Edge 12
  • Safari
  • Opera


My Personal Notes arrow_drop_up

Technical Writer Lets have an update

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.