If you are a JavaScript dev, you must have come across a situation when you wanted to check if an object has a certain property or not, and for that, you might have used, the two most popular options for that - the “in” operator and the “hasOwnProperty” method in JavaScript.
But, what is the key difference between the “in” operator and the “hasOwnProperty” method in JavaScript? and as a JS Developer, it is really important to know that. Why? because not knowing this difference can result in a real bad Bug.
1) “in” operator
The ”in“ operator returns true if the specified property is in the specified object or its prototype chain.
2) obj.hasOwnProperty
The hasOwnProperty method returns a boolean indicating whether the object has the specified property as its own property (as opposed to inheriting it).