if ('key' in myObj)
And the inverse.
if (!('key' in myObj))
Be careful! The
in operator matches all object keys, including those in the object's prototype chain.
myObj.hasOwnProperty('key') to check an object's own keys and will only return
key is available on
Unless you have a specific reason to use the
in operator, using
myObj.hasOwnProperty('key') produces the result most code is looking for.
Read more… Read less…
You should use
hasOwnProperty. For example:
Note: If you are using ESLint, the above may give you an error for violating the no-prototype-builtins rule, in that case the workaround is as below: