Object.hasOwn()
Returns true if the object has the specified property as its own (not inherited) property.
Syntax
Object.hasOwn(obj, prop) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
obj | object | Yes | The object to test. |
prop | string | symbol | Yes | The property name to check. |
Returns
boolean — true if obj has prop as an own property; otherwise false.
Examples
console.log(Object.hasOwn({ a: 1 }, 'a'));
Output
true
console.log(Object.hasOwn({}, 'toString'));
Output
false
Notes
The recommended replacement for `obj.hasOwnProperty(prop)` - it works even when
the object has no prototype or shadows hasOwnProperty. Does not see inherited
properties.
Browser & runtime support
| Environment | Since version |
|---|---|
| chrome | 93 |
| firefox | 92 |
| safari | 15.4 |
| edge | 93 |
| node | 16.9 |