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

Object.hasOwn() - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

Object.hasOwn()

Baseline

20223

Object.hasOwn() true false

: Object.hasOwn() Object.hasOwnProperty()

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

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

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

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

js
Object.hasOwn(obj, prop)

obj

JavaScript

prop

true false

Object.hasOwn() null undefined true false in

Object.prototype.hasOwnProperty() null hasOwnProperty() Object.prototype.hasOwnProperty() Object.prototype.hasOwnProperty.call(obj, prop) 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 

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

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

// `in`  true 
"prop" in example; // returns true
"toString" in example; // returns true
"hasOwnProperty" in example; // returns 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 - not defined

hasOwnProperty()

Object.hasOwn() hasOwnProperty() hasOwnProperty() hasOwnProperty() false 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

ECMAScript 2027 LanguageSpecification
# sec-object.hasown


Web Proxy Viewer  |  New URL  |  Original Page