JS: Object.hasOwn

By Xah Lee. Date: . Last updated: .

(new in ECMAScript 2022)

Object.hasOwn(obj, prop)

return true if obj has own property prop. Else false.

console.log(Object.hasOwn({ "p": 1 }, "p"));
// true

Why Object.hasOwn

Object.hasOwn is a better version of Object.prototype.hasOwnProperty. It fixes the problem when object has no parent, or has property "hasOwnProperty".

/*
2026-01-17
example of why
Object.hasOwn
is better than
Object.prototype.hasOwnProperty
*/

const xx = {
 66: 66,
 hasOwnProperty: ((x) => {
  return x + " haha";
 }),
};

// check if xx has property 66
console.log(xx.hasOwnProperty(66));
// 66 haha

// check if xx has property 66
console.log(Object.hasOwn(xx, 66));
// true

JavaScript. Access Properties