JS: Object.prototype.hasOwnProperty 👎

By Xah Lee. Date: . Last updated: .
obj.hasOwnProperty(key)

Return true if object obj has own property key key (string or Symbol). Else, false.

💡 TIP: better is JS: Object.hasOwn. because it works on object that does not have parent, or objects that has their own property hasOwnProperty.

const xx = { "p": 1 };
console.log(xx.hasOwnProperty("p"));
console.log(xx.hasOwnProperty("y") === false);

Example. with Symbol Key

// check if a object has own property of a symbol key
const xx = Symbol();
const jj = {};
jj[xx] = 3;
console.log(jj.hasOwnProperty(xx));

Example. Parent Property

// Object.prototype.hasOwnProperty return false if the property is not own property

const xx = { "p": 1 };

// create a object yy, with parent xx
const yy = Object.create(xx);

console.log(yy.hasOwnProperty("p") === false);

JavaScript. Access Properties