hasOwnProperty() method returns
true if the specified property is a direct property of the object — even if the value is
undefined. The method returns
false if the property is inherited, or has not been declared at all. Unlike the
in operator, this method does not check for the specified property in the object's prototype chain.
Object, and hence inherit its methods. For example
Array is an
Object, so you can use
hasOwnProperty() method to check whether an index exists:
const fruits = ["Apple", "Banana", "Watermelon", "Orange"];
The method will not be available in objects where it is reimplemented, or on objects created using
Object.create(null) (as these don't inherit from
Object.prototype). Examples for these cases are given below.