[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwn [Back]  [Original]

Object.hasOwn() - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Object.hasOwn()

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2022 3.

Object.hasOwn() true . false .

: Object.hasOwn() Object.prototype.hasOwnProperty() .

In this article

const object1 = {
  prop: "exists",
};

console.log(Object.hasOwn(object1, "prop"));
//   : true

console.log(Object.hasOwn(object1, "toString"));
//   : false

console.log(Object.hasOwn(object1, "undeclaredPropertyValue"));
//   : false

js
Object.hasOwn(obj, prop)

obj

JavaScript

prop

String Symbol.

true. false

null undefined Object.hasOwn() true . false . in , .

null hasOwnProperty() Object.prototype.hasOwnProperty() . Object.prototype.hasOwnProperty.call(obj, prop) Object.prototype.hasOwnProperty() , Object.hasOwn() .

Object.hasOwn()

example prop .

js
const example = {};
Object.hasOwn(example, "prop"); // false - 'prop'  

example.prop = "exists";
Object.hasOwn(example, "prop"); // true - 'prop'  

example.prop = null;
Object.hasOwn(example, "prop"); // true -   null  

example.prop = undefined;
Object.hasOwn(example, "prop"); // true -   undefined  

vs

.

js
const example = {};
example.prop = "exists";

// `hasOwn`     true .
Object.hasOwn(example, "prop"); // true
Object.hasOwn(example, "toString"); // false
Object.hasOwn(example, "hasOwnProperty"); // false

// `in`         true .
"prop" in example; // true
"toString" in example; // true
"hasOwnProperty" in example; // true

.

js
const example = { foo: true, bar: true };
for (const name of Object.keys(example)) {
  // 
}

for...in Object.hasOwn() .

js
const example = { foo: true, bar: true };
for (const name in example) {
  if (Object.hasOwn(example, name)) {
    // 
  }
}

Array hasOwn() .

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

hasOwnProperty

hasOwn() hasOwnProperty . , hasOwnProperty() . hasOwnProperty() Object.hasOwn() .

js
const foo = {
  hasOwnProperty() {
    return false;
  },
  bar: "The dragons be out of office",
};

console.log(foo.hasOwnProperty("bar")); // false

console.log(Object.hasOwn(foo, "bar")); // true

null- . Object.prototype hasOwnProperty() .

js
const foo = Object.create(null);
foo.prop = "exists";

console.log(foo.hasOwnProperty("prop"));
// Uncaught TypeError: foo.hasOwnProperty is not a function

console.log(Object.hasOwn(foo, "prop")); // true

Specification
ECMAScript 2027 LanguageSpecification
# sec-object.hasown


Web Proxy Viewer  |  New URL  |  Original Page