Let’s consider following
To find if,
age property/key exist in
user object we have following two options
in operator which we can use as follows
This gives output of
obj.hasOwnProperty(prop) method returns a
true if the key exist in object otherwise
And, here is how we can use
hasOwnProperty() to check if
age exist in
Tip: hasOwnProperty() checks the objects own properties whereas in operator matches all object keys, including those in the object's prototype chain.
inoperator should be used to check if property exist in object itself or in its prototype chain.
obj.hasOwnProperty(prop)checks if the key exist in the object only.