Object.prototype.hasOwnProperty()
Superseded by Object.hasOwn in ES2022, for one reason: being a method, it can be missing or replaced on the object you are asking about.
Demo
The results match Object.hasOwn exactly — inherited properties are false, and a property whose value is null still counts as present. What the demo cannot show is the failure mode that made this method obsolete: it only works because the object inherits it from Object.prototype. Give the object a property of the same name, or create it with no prototype at all, and the call throws instead of answering.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| key | string | symbol | yes | The property key. Non-symbols are coerced to strings, so 1 and "1" are the same key. |
Return value
boolean — True if the key is an own property. Inherited properties give false — which is the entire point of it.
Common patterns
if (Object.hasOwn(o, key)) { }
if (Object.prototype.hasOwnProperty.call(o, key)) { }
for (const k in o) { if (!Object.hasOwn(o, k)) continue; }
Examples
Pitfalls
JSON.parse('{"hasOwnProperty":1}').hasOwnProperty("x")
Object.hasOwn(JSON.parse('{"hasOwnProperty":1}'), "x")
Object.create(null).hasOwnProperty("a")
Object.hasOwn(Object.create(null), "a")
o.hasOwnProperty(k)
Object.hasOwn(o, k)
class A { m() {} } new A().hasOwnProperty("m")
"m" in new A()
When to use
- Legacy code where it already appears and the objects are trusted
- Runtimes predating ES2022, via the borrowed-call form
- New code → Object.hasOwn
- Objects from untrusted input → the static form, always
- Null-prototype dictionaries → the method does not exist there
- You want inherited properties too → the in operator
Notes
FAQ
Not in new code. Object.hasOwn does the same job, cannot be shadowed by the object being tested, and works on null-prototype objects. The only reason to keep the old form is a runtime older than 2021, and even then the borrowed call is safer than the direct one.
Object.hasOwn(o, k); // modern Object.prototype.hasOwnProperty.call(o, k); // safe legacy