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.
<!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> |
Output:
- Before clicking the button:

- After clicking the button:

Example 2: This example checks the properties on an object of a class.
<!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> |
Output:
- Before clicking the button:

- After clicking the button:

Supported Browsers: The browsers supported by JavaScript hasOwnProperty() method are listed below:
- Google Chrome
- Firefox 1
- Internet Explorer
- Edge 12
- Safari
- Opera
Recommended Posts:
- JavaScript | Array.from() method
- JavaScript | Array map() Method
- JavaScript | Array from() Method
- JavaScript | exec() Method
- JavaScript | Sort() method
- JavaScript | Replace() Method
- JavaScript | getTime() Method
- JavaScript | Promise.all() Method
- JavaScript | padStart() Method
- JavaScript | compile() Method
- JavaScript | clearTimeout() & clearInterval() Method
- JavaScript | Array.find() Method
- Javascript | MouseEvent getModifierState() Method
- JavaScript | Array.findIndex() Method
- JavaScript | Array.splice() Method
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.




