Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I generally use the <a href="https://developer.mozilla.org/en/JavaScript/Reference/Operators/typeof" rel="noreferrer"><code>typeof</code></a> operator: </p> <pre><code>if (typeof obj.foo !== 'undefined') { // your code here } </code></pre> <p>It will return <code>"undefined"</code> either if the property doesn't exist or its value is <code>undefined</code>.</p> <p><sub>(See also: <a href="https://stackoverflow.com/questions/3420071/difference-between-undefined-and-not-being-defined-in-javascript/3420160#3420160">Difference between <code>undefined</code> and not being defined.</a>)</sub></p> <p>There are other ways to figure out if a property exists on an object, like the <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/HasOwnProperty" rel="noreferrer"><code>hasOwnProperty</code></a> method:</p> <pre><code>if (obj.hasOwnProperty('foo')) { // your code here } </code></pre> <p>And the <code>in</code> operator:</p> <pre><code>if ('foo' in obj) { // your code here } </code></pre> <p>The difference between the last two is that the <code>hasOwnProperty</code> method will check if the property exist <em>physically</em> on the object (the property is not inherited).</p> <p>The <code>in</code> operator will check on all the properties reachable up in the prototype chain, e.g.:</p> <pre><code>var obj = { foo: 'bar'}; obj.hasOwnProperty('foo'); // true obj.hasOwnProperty('toString'); // false 'toString' in obj; // true </code></pre> <p>As you can see, <code>hasOwnProperty</code> returns <code>false</code> and the <code>in</code> operator returns <code>true</code> when checking the <code>toString</code> method, this method is defined up in the prototype chain, because <code>obj</code> inherits form <code>Object.prototype</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload