Skip to solution
easyPhone Screen

What does Object.hasOwn() do differently from Object.prototype.hasOwnProperty.call(), and why is it the recommended modern replacement?

772 views
01

Understand the problem

Question presented to candidate: "What does Object.hasOwn() actually check, how is it different from calling hasOwnProperty directly on an object, and why would you reach for hasOwn() instead in new code?"

What a strong answer should cover:

  • Object.hasOwn(obj, prop) returns true only if prop exists directly on obj itself, not inherited via the prototype chain -- functionally equivalent to Object.prototype.hasOwnProperty.call(obj, prop).
  • The problem hasOwn() solves: calling obj.hasOwnProperty(prop) directly assumes obj actually inherits a working hasOwnProperty from Object.prototype. An object created with Object.create(null) has no prototype at all, so obj.hasOwnProperty is not a function, and the call throws a TypeError.
  • The same failure mode happens if an object has its own property literally named hasOwnProperty that shadows the inherited method with something that is not a function.
  • Object.hasOwn() sidesteps both cases entirely because it is a static method -- it never relies on the target object having (or not having overridden) its own hasOwnProperty.
  • It is distinct from the in operator, which returns true for inherited properties too, walking the whole prototype chain -- hasOwn() only ever reports own properties.

Clarifying questions expected:

  • "Should I compare this against the in operator as well, or just against hasOwnProperty.call()?"
  • "Is there a meaningful difference in behavior for array indices versus regular object keys?"

Code / implementation expected: Yes -- a runnable comparison including a null-prototype object and a shadowed hasOwnProperty property, both showing the real failure hasOwn() avoids.

objecthasownpropertyhasown
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for JavaScript fundamentals interview questions. Difficulty: Easy

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. Every result below was actually run on Node.js v24.19.0 — the printed output is re

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSObject.hasOwn() vs hasOwnProperty.call() vs the in operator (run directly)
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 15 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track