AlgoInsight

Q&A

What is the hasOwnProperty() method in JavaScript?

By AlgoInsight Team2026-06-07javascript

Overview

JavaScript objects inherit properties from their prototype chain. Sometimes you need to know whether a property belongs directly to an object versus being inherited. hasOwnProperty() answers exactly that. It's commonly used when iterating object keys or checking for a key without being fooled by inherited or prototype properties.

graph TD
    A["obj.hasOwnProperty('x')"] --> B{x is a direct own property?}
    B -->|yes| C[true]
    B -->|inherited via prototype| D[false]
    B -->|absent| D
    A -.modern, safer.-> E["Object.hasOwn(obj, 'x')"]
    style C fill:#1e1b4b,color:#fff

Basic Usage

const obj = { name: "Ada" };

obj.hasOwnProperty("name");      // true
obj.hasOwnProperty("toString");  // false - inherited from Object.prototype
obj.hasOwnProperty("age");       // false - doesn't exist

toString exists on obj (you can call obj.toString()), but it's inherited, not owned - so hasOwnProperty returns false.

hasOwnProperty vs the in Operator

The in operator checks the whole prototype chain; hasOwnProperty checks only own properties:

const obj = { name: "Ada" };

"name" in obj;                  // true
"toString" in obj;             // true  - inherited, but `in` still finds it
obj.hasOwnProperty("toString"); // false - not an OWN property

Use in for "can I access this property at all?"; use hasOwnProperty for "does the object itself define this?"

Common Use: Safe Iteration

When iterating with for...in (which includes inherited enumerable properties), filter to own properties:

for (const key in obj) {
  if (Object.prototype.hasOwnProperty.call(obj, key)) {
    console.log(key, obj[key]);   // only the object's own keys
  }
}

(In modern code, Object.keys(obj) already returns only own enumerable keys, avoiding the need for this guard.)

The Gotcha: Calling It Safely

hasOwnProperty can be unreliable if the object overrides it or has no prototype:

// 1. Object with a property literally named "hasOwnProperty"
const tricky = { hasOwnProperty: () => "oops" };
tricky.hasOwnProperty("x");   // calls the OWN function, not Object's → wrong result

// 2. Object created with null prototype has no hasOwnProperty at all
const bare = Object.create(null);
bare.hasOwnProperty("x");     // ❌ TypeError: not a function

The safe classic workaround is to borrow it from the prototype:

Object.prototype.hasOwnProperty.call(obj, "x");   // always works

The Modern Solution: Object.hasOwn() (ES2022)

Object.hasOwn(obj, prop) does the same check but avoids both gotchas - it works on null-prototype objects and isn't shadowed by an own property named hasOwnProperty:

Object.hasOwn(obj, "name");          // true
Object.hasOwn(bare, "x");            // false - no TypeError
Object.hasOwn(tricky, "x");          // correct - not fooled by the override

Prefer Object.hasOwn() in new code.

Why This Matters in Interviews

JS interviews use this to probe understanding of the prototype chain. Key points: hasOwnProperty checks own properties only, contrasting with in (which walks the prototype chain); the safe-call idiom Object.prototype.hasOwnProperty.call(obj, key) and why it's needed (shadowing, null-prototype objects); and that Object.hasOwn() is the modern, robust replacement. Filtering for...in with it (or preferring Object.keys) shows practical knowledge.

Common Pitfalls

References

More answers