obj.hasOwnProperty(prop) returns true when prop is an own property of obj. Inherited properties return false.
Objects made with Object.create(null) have no hasOwnProperty method. Prefer Object.hasOwn(obj, prop) in modern code, or call Object.prototype.hasOwnProperty.call(obj, prop).
Syntax
Call obj.hasOwnProperty() like this. Start with this line.
JavaScript
obj.hasOwnProperty(prop)
Call
What it does
obj.hasOwnProperty("name")
True if name is an own property.
Object.hasOwn(obj, "name")
Modern static alternative; safer for null-prototype objects.
Object.prototype.hasOwnProperty.call(obj, "name")
Safe call when obj might not inherit from Object.prototype.
Call on the object. prop is usually a string key (Symbols also work).
Library declarations
JavaScript
obj.hasOwnProperty(prop)
JavaScript
object.hasOwnProperty(v: PropertyKey): boolean
Parameters
Parameter
Description
prop
Required. The property name (string) or Symbol to test.
Return Value
Return Value
Description
boolean
true if the property is own; otherwise false.
JavaScript Examples
Own Property vs Inherited Property
Compare hasOwnProperty with the in operator.
JavaScript
var proto = { role: "guest" };
var user = Object.create(proto);
user.name = "Ada";
console.log(user.hasOwnProperty("name"));
console.log(user.hasOwnProperty("role"));
console.log("role" in user);