if (key in object) or if(object.hasOwnProperty(key)

Be careful – they won’t produce the same result.

in will also return true if key gets found somewhere in the prototype chain, whereas Object.hasOwnProperty (like the name already tells us), will only return true if key is available on that object directly (its “owns” the property).

Leave a Comment