JavaScript: 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.
const xx = { "p": 1 };
console.log(xx.hasOwnProperty("p"));
console.log(xx.hasOwnProperty("y") === false);

Example with a Symbol key.

// check if a object has own property of a symbol key
const ss = Symbol();
const jj = {};
jj[ss] = 3;
console.log(jj.hasOwnProperty(ss));
// 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);

[see Access Property]

JavaScript Check Property Existence

BUY ΣJS JavaScript in Depth