![]() |
VOOZH | about |
JavaScripthasOwn() method is used to check if the object has the specified property or not. It returns true if the property exists else false. This method was introduced as a replacement for the Object.hasOwnProperty() method. It is different from in operator as it does not check for inherited properties.
Object.hasOwn(obj, prop)This method takes two parameters
Example 1: This example uses the hasOwn() method to check if a property exists or not
Output:
true
true
false
Example 2: This example will compare the in operator and Object.hasOwn() method.
Output: The in operator returns true for even inherited properties like toString is an inherited method for all JavaScript objeccts.
true
true
false
true
Example 3: This method compares the hasOwnProperty() method and hasOwn() method.
Output: The hasOwnProperty() method does not work on null objects but hasOwn() method works. So it is better than hasOwnProperty() method