Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Object.hasOwn()

BaselineWidely available

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().

Try it

const object1 = {  prop: "exists",};console.log(Object.hasOwn(object1, "prop"));// Expected output: trueconsole.log(Object.hasOwn(object1, "toString"));// Expected output: falseconsole.log(Object.hasOwn(object1, "undeclaredPropertyValue"));// Expected output: false

Syntax

js
Object.hasOwn(obj, prop)

Parameters

obj

The JavaScript object instance to test.

prop

TheString name orSymbol of the property to test.

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.

js
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 undefined

Direct vs. inherited properties

The following example differentiates between direct properties and properties inherited through the prototype chain:

js
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; // true

Iterating over the properties of an object

To iterate over the enumerable properties of an object, youshould use:

js
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:

js
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:

js
const fruits = ["Apple", "Banana", "Watermelon", "Orange"];Object.hasOwn(fruits, 3); // true ('Orange')Object.hasOwn(fruits, 4); // false - not defined

Problematic 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:

js
const foo = {  hasOwnProperty() {    return false;  },  bar: "The dragons be out of office",};console.log(foo.hasOwnProperty("bar")); // falseconsole.log(Object.hasOwn(foo, "bar")); // true

It can also be used withnull-prototype objects. These do not inherit fromObject.prototype, and sohasOwnProperty() is inaccessible.

js
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")); // true

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-object.hasown

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp