Checking if variable is defined before accessing its properties
Are you looking for this?

var data = object &amp;&amp; object.attribute &amp;&amp; object.attribute.data || {};


Alternatively, you could create a simple function to safely index objects that may not be defined, such as:

function resolve(value) {
    return function(key) {
        return key ? resolve((value || {})[key]) : value;
    };
}


You'd use it like this:

var data = resolve(object)('attribute')('data')() || {};


If anything in the chain does not exist, it will return undefined. This might be useful if you have deeply nested and unpredictable objects, such as an object resulting from parsing JSON data from a remote resource.

http://jsfiddle.net/VHcdq/1/