The JavaScript Object.hasOwn() method
The Object.hasOwn() method returns true if an object has a property as its own (not inherited from the prototype). Object.hasOwn(obj, "name"). It's the modern, safer replacement for obj.hasOwnProperty("name"), which can break on objects without a prototype or with a shadowed method.
Overview
Object.hasOwn() checks whether a property belongs to an object directly, rather than being inherited through the prototype chain. Object.hasOwn(user, "email") is true only if user has its own email property. This matters when you need to distinguish an object's real data from inherited methods, or to safely check for a key that might hold a falsy value like 0 or "" (where if (obj.key) would be misleading).
It's the modern replacement for obj.hasOwnProperty("key"), which has two long-standing pitfalls. An object created with Object.create(null) has no prototype, so it doesn't have a hasOwnProperty method and calling it throws. And an object could define its own property named hasOwnProperty, shadowing the real one. Object.hasOwn() sidesteps both by being a static method you call on the Object constructor.
It complements the in operator, which is different: "key" in obj is true for inherited properties too. Use in when inherited members count, and Object.hasOwn() when you specifically want own properties — for example when iterating with for...in, which visits inherited keys.
Syntax
Object.hasOwn(obj, propertyKey)
Object.hasOwn({ a: 1 }, "a") // true
Object.hasOwn({ a: 1 }, "b") // false
Object.hasOwn({ a: 1 }, "toString") // false (inherited)
Parameters
The Object.hasOwn() method accepts the following parameters.
| Parameter | Description |
|---|---|
obj |
The object to test. |
propertyKey |
The property name (or symbol) to check for as an own property. |
Example
<pre id="out" style="font:15px ui-monospace,monospace"></pre>
<script>
const user = { name: 'Ada', score: 0 };
document.getElementById('out').textContent =
'has name? ' + Object.hasOwn(user, 'name') + '\n' +
'has score? ' + Object.hasOwn(user, 'score') + '\n' +
'has email? ' + Object.hasOwn(user, 'email');
// true / true / false
</script>
Best practices
- Use
Object.hasOwn(obj, key)instead ofobj.hasOwnProperty(key)— it's safer. - Reach for it to check keys that might hold falsy values, where
if (obj.key)is unreliable. - Use it to skip inherited keys inside a for...in loop.
- Use the
inoperator instead when inherited properties should also count.
Frequently asked questions
What does Object.hasOwn() do?
true if an object has the given property as its own (not inherited), and false otherwise.What is the difference between Object.hasOwn() and hasOwnProperty()?
Object.hasOwn() is a static method that works even on prototype-less objects and can't be shadowed, so it's the safer modern choice.What is the difference between Object.hasOwn() and the in operator?
Object.hasOwn() only counts own properties; "key" in obj is also true for inherited ones.Why use Object.hasOwn() instead of if (obj.key)?
if (obj.key) is false when the value is falsy (0, "", false) even if the key exists. Object.hasOwn() checks existence, not truthiness.