Object.prototype.hasOwnProperty()
The hasOwnProperty()
method of Object
instances returns a boolean indicating whether this object has the specified property as its own property (as opposed to inheriting it).
Note: Object.hasOwn()
is recommended over hasOwnProperty()
, in browsers where it is supported.
Return value
Returns true
if the object has the specified property as own property; false
otherwise.
Description
The hasOwnProperty()
method returns true
if the specified property is a direct property of the object — even if the value is null
or undefined
. The method returns false
if the property is inherited, or has not been declared at all. Unlike the in
operator, this method does not check for the specified property in the object's prototype chain.
The method can be called on most JavaScript objects, because most objects descend from Object
, and hence inherit its methods. For example Array
is an Object
, so you can use hasOwnProperty()
method to check whether an index exists:
const fruits = ["Apple", "Banana", "Watermelon", "Orange"];
fruits.hasOwnProperty(3);
fruits.hasOwnProperty(4);
The method will not be available in objects where it is reimplemented, or on null
-prototype objects (as these don't inherit from Object.prototype
). Examples for these cases are given below.
Using hasOwnProperty to test for an own property's existence
The following code shows how to determine whether the example
object contains a property named prop
.
const example = {};
example.hasOwnProperty("prop");
example.prop = "exists";
example.hasOwnProperty("prop");
example.prop = null;
example.hasOwnProperty("prop");
example.prop = undefined;
example.hasOwnProperty("prop");
Direct vs. inherited properties
The following example differentiates between direct properties and properties inherited through the prototype chain:
const example = {};
example.prop = "exists";
example.hasOwnProperty("prop");
example.hasOwnProperty("toString");
example.hasOwnProperty("hasOwnProperty");
"prop" in example;
"toString" in example;
"hasOwnProperty" in example;
Iterating over the properties of an object
The following example shows how to iterate over the enumerable properties of an object without executing on inherited properties.
const buz = {
fog: "stack",
};
for (const name in buz) {
if (buz.hasOwnProperty(name)) {
console.log(`this is fog (${name}) for sure. Value: ${buz[name]}`);
} else {
console.log(name);
}
}
Note that the for...in
loop only iterates enumerable items: the absence of non-enumerable properties emitted from the loop does not imply that hasOwnProperty
itself is confined strictly to enumerable items (as with Object.getOwnPropertyNames()
).
Using hasOwnProperty as a property name
JavaScript does not protect the property name hasOwnProperty
; an object that has a property with this name may return incorrect results:
const foo = {
hasOwnProperty() {
return false;
},
bar: "Here be dragons",
};
foo.hasOwnProperty("bar");
The recommended way to overcome this problem is to instead use Object.hasOwn()
(in browsers that support it). Other alternatives include using an external hasOwnProperty
:
const foo = { bar: "Here be dragons" };
Object.hasOwn(foo, "bar");
Object.prototype.hasOwnProperty.call(foo, "bar");
({}).hasOwnProperty.call(foo, "bar");
Note that in the first two cases there are no newly created objects.
Objects created with Object.create(null)
null
-prototype objects do not inherit from Object.prototype
, making hasOwnProperty()
inaccessible.
const foo = Object.create(null);
foo.prop = "exists";
foo.hasOwnProperty("prop");
The solutions in this case are the same as for the previous section: use Object.hasOwn()
by preference, otherwise use an external object's hasOwnProperty()
.
Specifications
Browser compatibility
|
Desktop |
Mobile |
Server |
|
Chrome |
Edge |
Firefox |
Opera |
Safari |
Chrome Android |
Firefox for Android |
Opera Android |
Safari on IOS |
Samsung Internet |
WebView Android |
Deno |
Node.js |
hasOwnProperty |
1 |
12 |
1 |
5 |
3 |
18 |
4 |
10.1 |
1 |
1.0 |
4.4 |
1.0 |
0.10.0 |