0

Is there a short way for accessing a nested object without checking each parent object?

this will throw an exception if foo or bar is undefined:

var exists = (foo.bar.value !== undefined);

I would expect a check function like:

var exists = Object.exists(foo.bar.value);

Is there already something build-in javascript?

Mr. Smith
  • 559
  • 1
  • 5
  • 17

1 Answers1

0

Use typeOf

if (typeof myObject!= "undefined") {
   console.log('It exists')
}
Alexis
  • 5,681
  • 1
  • 27
  • 44
Jackthomson
  • 644
  • 8
  • 23
  • 1
    That doesn't answer OP's question : `typeof foo.bar.value` when `foo` or `bar` are undefined will still raise a ReferenceError rather than return a value that can be interpreted as an absence of value. – Aaron Nov 17 '16 at 14:16