Object.hasOwn(obj, prop) returns true when prop is an own property of obj. Inherited properties from the prototype chain return false.
Prefer Object.hasOwn over obj.hasOwnProperty(prop). It is a static call, works on objects made with Object.create(null), and cannot be broken by a shadowed hasOwnProperty method.
Syntax
Call Object.hasOwn() like this. Start with this line.
JavaScript
Object.hasOwn(obj, prop)
Call
What it does
Object.hasOwn(obj, prop)
Returns true if prop is an own property of obj.
Built into JavaScript. Call on Object — no import required.
Library declarations
JavaScript
Object.hasOwn(obj, prop)
JavaScript
hasOwn(o: object, v: PropertyKey): boolean;
Parameters
Parameter
Description
obj
Required. The object to test. Must be an object (not null or undefined).
prop
Required. The property key to look for (string or symbol).
Return Value
Return Value
Description
boolean
true if the property exists directly on the object; false if it is missing or only inherited.
JavaScript Examples
Example 1 — Basic Use
Check own properties on a simple object.
JavaScript
var user = { name: "Ada", age: 36 };
console.log(Object.hasOwn(user, "name"));
console.log(Object.hasOwn(user, "email"));
console.log(Object.hasOwn(user, "toString"));