Rewriting clumsy code that gets the value of a nested property in JS object
If you're certain the token exists in either of the objects you can do: 

First check for the existence of the value in data. 

var value = data.d || data.value;


then access the value to get the token

var token = value &amp;&amp; value.GetContextWebInformation.FormDigestValue;




var value = data.d || data.value;

var token = value &amp;&amp; value.GetContextWebInformation.FormDigestValue;


if (token) {
  this.options.token = token;
}




Otherwise, you can use getIn function:



function getIn(pathArray, obj) {
  var tmp = obj;
  for (var i = 0; i &lt; pathArray.length; i++) {
    if (tmp.hasOwnProperty(pathArray[i])) {
      tmp = tmp[pathArray[i]];
      continue;
    }
    return undefined;
  }
  return tmp;
}

var token = getIn(['d', 'GetContextWebInformation', 'FormDigestValue'], data) ||
            getIn(['value', 'GetContextWebInformation', 'FormDigestValue'], data);

if (token) {
  this.options.token = token;
}