As the name suggests,
toString will not be identified by
hasOwnProperty. These MDN docs give a good overview of
hasOwnProperty method, the
in operator also checks the object's inherited properties, so methods like
toString present in the prototype will be identified.
Third approach: check if property is not
This approach is similar to the
in operator, however properties may be exist on the object but actually have a value of
undefined, in which case this approach will return
false. Depending on the usecase, this may or may not be desirable.
This blog post by Dmitri Pavlutin explains these three approaches in greater detail.