I'm trying to evaluate whether an object has a certain property like the following, as if I go directly to the last 'if
', I might get undefined
due to property1
or property2
being non-existent:
if (obj[property1]) {
if (obj[property1][property2]) {
if (obj[property1][property2][property3]) {
console.log('property exists!')
}
}
}
I'm working with numbers. This will work if the VALUE of property3
is not 0, since 0 evaluates to falsy.
{
property1: {
property2: {
property3: 0 // evaluates to false as 0 is falsy
}
}
}
How can I check if a object property exists regardless of its value?