title: Elegant Javascript code for checking undefined values
body: <p>So I have a callback function which provides a json object:</p>
  
  <pre><code>function(object){
      var data=object.packet.link.ip.tcp.data;
      console.log(data.toString());
  }
  </code></pre>
  
  <p>The problem I have is that any of packet/link/ip/tcp/data can be "undefined" - my node.js program falls over every time it hits an undefined variable.</p>
  
  <p>I tried putting the declaration of "data" inside try/catch, however I keep getting "undefined" errors - my guess is that I'd need to put object/object.packet/object.packet.link/object.packet.link.ip/etc.  in try/catch.</p>
  
  <p>So, I found some elegant coffeescript:</p>
  
  <pre><code>if object?.packet?.link?.ip?.tcp?.data? then doStuff()
  </code></pre>
  
  <p>which compiles to:</p>
  
  <pre><code>var _ref, _ref1, _ref2, _ref3;
  
  if ((typeof object !== "undefined" &amp;&amp; object !== null ? (_ref = object.packet) != null ? (_ref1 = _ref.link) != null ? (_ref2 = _ref1.ip) != null ? (_ref3 = _ref2.tcp) != null ? _ref3.data : void 0 : void 0 : void 0 : void 0 : void 0) != null) {
         //doStuff:
         var data = object.packet.link.ip.tcp.data;
         console.log(data.toString());
  }
  </code></pre>
  
  <p>Yuck!</p>
  
  <p>Now it works perfectly, but I was just wondering if there's a more elegant (readable) way of doing this in pure Javascript?</p>