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

javascript

Solution

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).

Problem

Do the following two statements produce the same output? Is there any reason to prefer one way to the other? ``` if (key in object) if (object.hasOwnProperty(key)) ```

Original source