title: Access Javascript nested objects safely
body: <p>I have json based data structure with objects containing nested objects. In order to access a particular data element I have been chaining references to object properties together. For example:</p>
  
  <pre><code>var a = b.c.d;
  </code></pre>
  
  <p>If b or b.c is undefined, this will fail with an error. However, I want to get a value if it exists otherwise just undefined. What is the best way to do this without having to check that every value in the chain exists?</p>
  
  <p>I would like to keep this method as general as possible so I don't have to add huge numbers of helper methods like:</p>
  
  <pre><code>var a = b.getD();
  </code></pre>
  
  <p>or</p>
  
  <pre><code>var a = helpers.getDFromB(b);
  </code></pre>
  
  <p>I also want to try to avoid try/catch constructs as this isn't an error so using try/catch seems misplaced. Is that reasonable?</p>
  
  <p>Any ideas?</p>