JavaScript
Check object property with Object.hasOwn()
The Object.hasOwn()
method can be used to check if an object contains a property as it's own
property. Inherited properties and properties from the object's prototype chain will be ignored.
It is a more robust replacement for Object.prototype.hasOwnProperty()
.
const obj = {
foo: 'foo',
bar: undefined,
baz: null
};
console.log(Object.hasOwn(obj, 'foo')) // true
console.log(Object.hasOwn(obj, 'bar')) // true
console.log(Object.hasOwn(obj, 'baz')) // true
console.log(Object.hasOwn(obj, 'toString')) // false