JS: Object.hasOwn β™ 

By Xah Lee. Date: . Last updated: .

(new in ECMAScript 2022)

Object.hasOwn(obj, key)

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

console.assert(
 Object.hasOwn({ dog: 1 }, "dog"),
);

Object .prototype .hasOwnProperty vs Object.hasOwn

/*
example of
Object.prototype.hasOwnProperty
return incorrect result, due to property name collision.

Object.hasOwn no have this problem.
*/

const xx = { "dog": 3, "hasOwnProperty": (x) => false };

// check if xx has property dog
console.assert(xx.hasOwnProperty("dog") === false);

// check if xx has property dog
console.assert(Object.hasOwn(xx, "dog"));