Say that I have an object that is currently undefined
myObject.property1
It would make sense that any children key off of something that's undefined should also evaluate as undefined, right?
myObject.property1.description
But when I go into the JS console I do:
myObject.property1
undefined
myObject.property1.description
TypeError: Cannot read property 'description' of undefined
Where this gets messy is if I'm doing a conditional:
if(myObject.property1.description){
console.log("it is defined!");
}else{
console.log("it is not defined!");
}
I would expect it to console log it is not defined!
but instead I get the error and the conditional just flat-out fails. I want it to evaluate as undefined though.
- Why doesn't it return
undefined
as well? - I want the conditional to check for the
existence
of the value. How do I do this?