What's the shortest syntax to check if jsonObject is not undefined before accessing its errorMessage property?
var jsonObject = SomeMethodReturningAnObject();
if (jsonObject.errorMessage === undefined) // jsonObject can be undefined and this will throw an error
/* success! */
else
alert(jsonObject.errorMessage);
You can use the &&
operator, since it doesn't evaluate the right-hand side if the left-hand side is undefined
:
if (jsonObject && jsonObject.errorMessage === undefined)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With