I have a function which parses the address components of the Google Maps API JSON
and then returns the city / locality / route name.
The getAddressComponent()
returns a null
if it cannot find the key.
let route = getAddressComponent(addressComponents, 'route').value.long_name;
So let's say it didn't find the key, then I get a Error: TypeError: Cannot read property 'long_name' of undefined
obviously because it's not defined.
How do I check for null
in javascript other than the conditional method (a === null)
?
How can I simply check like this with ?
EDIT : Safe Navigation Operator
let route = getAddressComponent(addressComponents, 'route')?.value.long_name;
And if it doesn't exists, it could probably set route
to null
instead of throwing a Error ?
(element != null ? element.value.long_name : null)
? – Hippel