I'm currently null checking an object structure like so:
if (error.response && error.response.data && error.response.data.errorMessage)
{
// Do stuff
}
All I really want to do is null check the deepest variable error.response.data.errorMessage
, but because I want to avoid null pointer exceptions in the check I have to first check error.response
, and then error.response.data
.
Is there a cleaner way to do this? I imagine as you want to query deeper and deeper into the hierarchy it can get quite messy.
Thanks in advance!
CodePudding user response:
You can use optional chaining:
if (error.response?.data?.errorMessage) {
// Do stuff
}