DEV Community

Discussion on: 5 object methods you must know as a javascript Developer

Collapse
kahn profile image
Karim Hussain • Edited on

One of my most often used methods is the Object.prototype.hasOwnProperty() to check if the object has a specific property.

This allows you to avoid reference exceptions.

Note: Make sure you use this by calling the method from the Object prototype like this: Object.hasOwnProperty.call(obj, "property")

Why you might ask? It is because if the object you are trying to call this method from is null you will run into an exception. By making use of call you can avoid this from happening.

A very rare scenario would also be that if you call hasOwnProperty from the object itself, the method could have been mutated.

Enjoy :)