Update
Optional chaining is now part of the ECMAScript spec and can be used on most javascript clients (browsers, node.js, etc)
x = a.b?.c?.d?.e?.f?.g
To use a default value if the access fails you can use the Nullish coalescing operator (??)
x = a.b?.c?.d?.e?.f?.g ?? 'my default value'
original answer (2017)
The easiest way is to use try catch
try {
x = a.b.c.d.e.f.g
} catch(e) {
x = undefined;
}
There is a proposal for this called optional chaining
you can check it here: https://github.com/tc39/proposal-optional-chaining
x = a.b?.c?.d?.e?.f?.g
If you are using a transpiler you'll be able to use it, however its still in the very early stages and might not be accepted to be supported in the spec