Object.hasOwn()
Object.hasOwn(obj, prop) Updated March 13, 2026 · Object Methods
object property-check es2022
Object.hasOwn() is a static method that returns true if the specified object has the indicated property as its own property. If the property is inherited or does not exist, it returns false.
Syntax
Object.hasOwn(obj, prop)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
| obj | object | — | The object to check |
| prop | string | symbol | — | The property name or Symbol to check |
Examples
Basic usage
const obj = { name: 'Alice', age: 30 };
console.log(Object.hasOwn(obj, 'name'));
// true
console.log(Object.hasOwn(obj, 'age'));
// true
console.log(Object.hasOwn(obj, 'gender'));
// false
Inherited properties return false
const parent = { a: 1 };
const child = Object.create(parent);
child.b = 2;
console.log(Object.hasOwn(child, 'a'));
// false — inherited from parent
console.log(Object.hasOwn(child, 'b'));
// true — own property
Checking Symbol properties
const sym = Symbol('id');
const obj = { [sym]: 123 };
console.log(Object.hasOwn(obj, sym));
// true
Common Patterns
Filtering own properties in loops
const parent = { inherited: 1 };
const child = Object.create(parent);
child.own = 2;
child.another = 3;
for (const key in child) {
if (Object.hasOwn(child, key)) {
console.log(key); // logs: 'own', 'another'
}
}
Safe property checking before deletion
function safeDelete(obj, prop) {
if (Object.hasOwn(obj, prop)) {
delete obj[prop];
return true;
}
return false;
}
Object.hasOwn() vs ObjecthasOwnProperty()
Object.hasOwn() is essentially a replacement for ObjecthasOwnProperty(). Key differences:
- Syntax:
Object.hasOwn(obj, prop)vsobj.hasOwnProperty(prop) - Null safety: Object.hasOwn() throws TypeError if obj is null/undefined (unlike hasOwnProperty which would silently fail)
- Modern: Object.hasOwn() is the recommended approach for new code
const obj = { a: 1 };
// Both work for normal objects
obj.hasOwnProperty('a'); // true
Object.hasOwn(obj, 'a'); // true
// But with null/undefined:
Object.hasOwn(null, 'a'); // TypeError
Object.hasOwn(undefined, 'a'); // TypeError
See Also
- object::keys — Get object keys
- object::entries — Get object entries