However, document.querySelector will throw if the browser does not recognize #alphaDiv as a valid selector. But the probability of encountering a browser which doesn't support ID selectors is probably negligible. As an alternative you can consider document.getElementById. It will be faster, and should never throw.

Excluding querySelector errors, you can check that every value on which you attempt to retrieve a property or which you attempt to call really exists. Something like