Object.hasOwn()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2022.
TheObject.hasOwn() static method returnstrue if the specified object has the indicated property as itsown property. If the property is inherited, or does not exist, the method returnsfalse.
Note:Object.hasOwn() is intended as a replacement forObject.prototype.hasOwnProperty().
In this article
Try it
const object = { prop: "exists",};console.log(Object.hasOwn(object, "prop"));// Expected output: trueconsole.log(Object.hasOwn(object, "toString"));// Expected output: falseconsole.log(Object.hasOwn(object, "undeclaredPropertyValue"));// Expected output: falseSyntax
Object.hasOwn(obj, prop)Parameters
Return value
true if the specified object has directly defined the specified property. Otherwisefalse
Description
TheObject.hasOwn() method returnstrue if the specified property is a direct property of the object — even if the property value isnull orundefined. The method returnsfalse if the property is inherited, or has not been declared at all. Unlike thein operator, this method does not check for the specified property in the object's prototype chain.
It is recommended overObject.prototype.hasOwnProperty() because it works fornull-prototype objects and with objects that have overridden the inheritedhasOwnProperty() method. While it is possible to workaround these problems by accessingObject.prototype.hasOwnProperty() on another object (likeObject.prototype.hasOwnProperty.call(obj, prop),Object.hasOwn() is more intuitive and concise.
Examples
>Using Object.hasOwn() to test for a property's existence
The following code shows how to determine whether theexample object contains a property namedprop.
const example = {};Object.hasOwn(example, "prop"); // false - 'prop' has not been definedexample.prop = "exists";Object.hasOwn(example, "prop"); // true - 'prop' has been definedexample.prop = null;Object.hasOwn(example, "prop"); // true - own property exists with value of nullexample.prop = undefined;Object.hasOwn(example, "prop"); // true - own property exists with value of undefinedDirect vs. inherited properties
The following example differentiates between direct properties and properties inherited through the prototype chain:
const example = {};example.prop = "exists";// `hasOwn` will only return true for direct properties:Object.hasOwn(example, "prop"); // trueObject.hasOwn(example, "toString"); // falseObject.hasOwn(example, "hasOwnProperty"); // false// The `in` operator will return true for direct or inherited properties:"prop" in example; // true"toString" in example; // true"hasOwnProperty" in example; // trueIterating over the properties of an object
To iterate over the enumerable properties of an object, youshould use:
const example = { foo: true, bar: true };for (const name of Object.keys(example)) { // …}But if you need to usefor...in, you can useObject.hasOwn() to skip the inherited properties:
const example = { foo: true, bar: true };for (const name in example) { if (Object.hasOwn(example, name)) { // … }}Checking if an Array index exists
The elements of anArray are defined as direct properties, so you can usehasOwn() method to check whether a particular index exists:
const fruits = ["Apple", "Banana", "Watermelon", "Orange"];Object.hasOwn(fruits, 3); // true ('Orange')Object.hasOwn(fruits, 4); // false - not definedProblematic cases for hasOwnProperty()
This section demonstrates thatObject.hasOwn() is immune to the problems that affecthasOwnProperty(). Firstly, it can be used with objects that have re-implementedhasOwnProperty(). In the example below, the re-implementedhasOwnProperty() method reports false forevery property, but the behavior ofObject.hasOwn() remains unaffected:
const foo = { hasOwnProperty() { return false; }, bar: "The dragons be out of office",};console.log(foo.hasOwnProperty("bar")); // falseconsole.log(Object.hasOwn(foo, "bar")); // trueIt can also be used withnull-prototype objects. These do not inherit fromObject.prototype, and sohasOwnProperty() is inaccessible.
const foo = Object.create(null);foo.prop = "exists";console.log(foo.hasOwnProperty("prop"));// Uncaught TypeError: foo.hasOwnProperty is not a functionconsole.log(Object.hasOwn(foo, "prop")); // trueSpecifications
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-object.hasown> |