<p>How do I check if an object has a specific property in JavaScript?</p>

<p>Consider:</p>

<pre><code>x = {'key': 1};
if ( x.hasOwnProperty('key') ) {
    //Do this
}
</code></pre>

<p>Is that the best way to do it?</p>

Yes it is :)  I think you can also do
```
Object.prototype.hasOwnProperty.call(x, 'key')
```
which should also work if
```
x
```
has a property called
```
hasOwnProperty
```
:)
But that tests for own properties.  If you want to check if it has an property that may also be inhered you can use
```
typeof x.foo != 'undefined'
```
.