How to check if object within object exists
It can be done simply using the code below:
var newVal = (foo && foo.bar && typeof foo.bar.myVal !== 'undefined') ? foo.bar.myVal : foo.bar.myVal
A property is null or undefined, it will be evaluated as false so the above code will only process up to the first 'false' statement.
var newVal = ('foo' in window && // could be typeof foo !== 'undefined' if you want all scopes
'bar' in foo &&
'myVal' in foo.bar) ? foo.bar.myVal : null;
To be fair to javascript, that reads almost like natural language.
The simplest test is:
if (foo && foo.bar) {
// play with foo.bar.myVal ...
}