Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
While the in operator can check
for properties of an object, including properties from the prototype
chain, the hasOwnProperty method can
check an object for a property that is not from the prototype
chain.
Below, we want to know if myObject contains the property foo, and that it is not inheriting the property
from the prototype chain. To do this, we ask if myObject has its own property called foo.
<!DOCTYPE html><html lang="en"><body><script>
var myObject = {foo: 'value'};
console.log(myObject.hasOwnProperty('foo')) // logs true
// vs. a property from the prototype chain
console.log(myObject.hasOwnProperty('toString'); // logs false
</script></body></html>