If you are only fetching the stateDescription metadata then any item that does not have that particular metadata will also have undefined metadata. You have to test for that (or that and stateDescription if you have fetched more than one metadata namespace):
the first therm: loop.item.metadata !== undefined
is to test whether or not the metadata returned has any content at all
the second therm loop.item.metadata.stateDescription !== undefined
will check if the requested metadata namespace exist. This will return an error if no metadata namespace was returned.
Yes, that’s correct. The second one returns an error if there is no metadata at all, because expressions are allowed to return undefined as a value but you cannot ask the parser to perform any actions on an undefined value (how would it know what action to perform if there is no context from a value type?). So if there’s any possibility that a value you are trying to work with can be undefined, you always want to test that it exists before you try and do anything with it.