title: javascript: best way of checking if object has a certain element or property?
body: <p>Suppose I have this: </p>
  
  <pre class="lang-js prettyprint-override"><code>var person = { "name": "John Doe", "email": "john.doe@example.com" };
  </code></pre>
  
  <p>This object only has two elements called <code>name</code> and <code>email</code>. Some persons also have an element <code>age</code>, but this particular person doesn't. What's the best way to check this?</p>
  
  <ul>
  <li><code>if (person.age) { ... }</code></li>
  <li><code>if (person.age != undefined) { ... }</code></li>
  <li><code>if (person.age !== undefined) { ... }</code></li>
  <li><code>if (typeof(person.age) != 'undefined') { ... }</code></li>
  <li><code>if (person.hasOwnProperty('age')) { ... }</code></li>
  </ul>
  
  <p>I know all these don't do the same, e.g. <code>if (person.age)</code> would also fail if <code>age</code> <em>does</em> exist but it's <code>false</code> or <code>null</code> or <code>''</code> or <code>0</code>. And I wonder if some aren't just flat out wrong. </p>
  
  <p>Note that <code>person</code> is known to be an existing object here, but <code>person.age</code> may or may not exist.</p>