What is property in hasOwnProperty in JavaScript?

hasOwnProperty returns a boolean value indicating whether the object on which you are calling it has a property with the name of the argument. For example: var x = { y: 10 }; console.log(x.hasOwnProperty(“y”)); //true console.log(x.hasOwnProperty(“z”)); //false However, it does not look at the prototype chain of the object. It’s useful to use it when … Read more

Why use Object.prototype.hasOwnProperty.call(myObj, prop) instead of myObj.hasOwnProperty(prop)?

Is there any practical difference [between my examples]? The user may have a JavaScript object created with Object.create(null), which will have a null [[Prototype]] chain, and therefore won’t have hasOwnProperty() available on it. Using your second form would fail to work for this reason. It’s also a safer reference to Object.prototype.hasOwnProperty() (and also shorter). You … Read more