title: Check if a object is defined, best practice.
body: <p>I have the following JSON response from a ajax-request.</p>
  
  <pre><code>var json = {
      "response": {
          "freeOfChargeProduct": {  
          "description": "Product",  
          "orderQty": 5,
          "productName": "XYZ",
          "qty": 6,
          "details": {
              "price": 55.5, 
              "instock": "true",
              "focQuantity": 1
          }
      }, 
      "orderLineId": 4788,
      "totalOrderLinePrice": "741.36",
      "totalOrderPrice": "1,314.92",
      "totalQty": 17
  };
  </code></pre>
  
  <hr>
  
  <p>The JSON dosen't always return a "freeOfChargeProduct" property. So if I want to get the "freeOfChargeProduct" price, then I have to do the following:</p>
  
  <pre><code>var getFreeOfChargeProductPrice = function() { 
     var r = json.response;
     if (r &amp;&amp; r.freeOfChargeProduct &amp;&amp; r.freeOfChargeProduct.details) {
        return r.freeOfChargeProduct.details.price;         
     }
     return null;
  };
  </code></pre>
  
  <hr>
  
  <p>No problems. But it's very annoying to check every property in the object, so I created a function that check if a property in a object is defined.</p>
  
  <pre><code>var getValue = function (str, context) {
      var scope = context || window,
          properties = str.split('.'), i;
      for(i = 0; i &lt; properties.length; i++) {
        if (!scope[properties[i]]) {                       
           return null;
        } 
        scope = scope[properties[i]];        
      }
      return scope;
  };
  </code></pre>
  
  <hr>
  
  <pre><code>var price = getValue('json.response.freeOfChargeProduct.details.price');
  // Price is null if no such object exists.
  </code></pre>
  
  <hr>
  
  <p>Now to my question: Is this a good or bad way to check if a property exists in an object? Any better suggestions/methods?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p><em>I don't wan't to use the &amp;&amp;-operator. I am lazy and I'm looking for a reusable method to check if a object (or property of a object) is defined.</em></p>
  
  <p>:) Thanks!</p>