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
  • 459
  • 1
  • 5
  • 16

1 Answers1

0

Use typeOf

if (typeof myObject!= "undefined") {
   console.log('It exists')
}
Alexis
  • 5,481
  • 1
  • 26
  • 43
Jackthomson
  • 634
  • 8
  • 22
  • 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