0

So, there's about a billion questions here on how to check if an object property in JavaScript is defined or not. They work great. Well, until I have a situation like this:

var obj = {
    prop1: {
        prop1a: {
            prop1aa: "somestring"
        }
    }
};

//conditional stuff happens, potentially changing obj's structure

//Use your method of choice for checking if this is defined or not:
if (typeof obj.prop2.prop2a.prop2aa !== "undefined") {
    //prop2aa is defined, do something with it!
}

However, since prop2 itself isn't defined in some situations, this check throws an error in those situations. Is there a robust way to check if a distant property is defined in an object without resorting to a series of if checks at every single level of the object on the way to the desired property?

Ellesedil
  • 1,576
  • 1
  • 20
  • 44

0 Answers0