The Wayback Machine - https://web.archive.org/web/20250118105949/https://www.geeksforgeeks.org/javascript-object-hasownproperty-method/
Open In App

JavaScript Object hasOwnProperty() Method

Last Updated : 26 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The hasOwnProperty() method in JavaScript checks if an object has a specific property as its own (not inherited). It returns true if the property exists directly on the object, otherwise false, making it useful for distinguishing own properties from inherited ones.

Syntax

object.hasOwnProperty( prop );

Parameters:

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

Example 1: In this example, the hasOwnProperty() method checks if the exampleObj contains the properties “height” (which exists) and “breadth” (which doesn’t exist). It returns true for “height” and false for “breadth”.

JavaScript
let exampleObj = {};
exampleObj.height = 100;
exampleObj.width = 100;

// Checking for existing property
let result1 = exampleObj.hasOwnProperty("height");

// Checking for non-existing property
let result2 = exampleObj.hasOwnProperty("breadth");

console.log(result1);  // true
console.log(result2);  // false

Output
true
false

Example 2: In this example The checkProperty() function creates a Car object and checks if the model property exists (true) and if the wheels property doesn’t exist (false) using the hasOwnProperty() method.

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");

    console.log(result1);

    console.log(result2);
}
checkProperty()

Output
true
false

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

Supported Browsers:

  • Google Chrome
  • Firefox
  • Edge
  • Safari
  • Opera

JavaScript Object hasOwnProperty() Method- FAQs

What is hasOwnProperty() in JavaScript Object?

JavaScript hasOwnProperty() is a method used to check if an object has a specific property as its own (not inherited from its prototype chain). Returns true or false.

How does the hasOwnProperty() method work?

The hasOwnProperty() method checks if the object has the specified property as a direct property. It returns true if the property is found directly on the object and false otherwise.

Does the hasOwnProperty() method check inherited properties?

No, the hasOwnProperty() method does not check inherited properties. It only checks if the property exists directly on the object.

What will happen if hasOwnProperty() is called on null or undefined?

Calling hasOwnProperty() on null or undefined will throw a TypeError because these values are not objects.

How does the hasOwnProperty() method handle non-string properties?

The hasOwnProperty() method converts non-string properties to strings before checking. For example, obj.hasOwnProperty(1) is equivalent to obj.hasOwnProperty(“1”).

What are some common use cases for the hasOwnProperty() method?

Common use cases for the hasOwnProperty() method include:

  • Checking for the existence of a property before accessing or modifying it.
  • Avoiding unintended access to properties inherited from the prototype chain.
  • Validating object structures in dynamic or complex applications.

Next Article

Similar Reads

three90RightbarBannerImg