Errors are not something to be scared of. Every program that runs in production handles errors on a constant basis. Our job as developers is to handle the errors gracefully and in a way that aligns with our expectations. Now, I will admit, one of the big criticisms I have of JavaScript is how hard it is to know whether I should expect a function to potentially throw an error or not.
In Go and some other languages, the function signature tells us if we should expect an error:
func getMovieRecord(movieId int) (Movie, error) {
// ...
}
This lets us know if we should be prepared to handle an error when we call a function. In JavaScript... we're kinda left guessing. The only way to know for sure is to read the body of the function. This might tempt you to just wrap everything in tons of try/catch blocks, but I'd advise against that.
Here are some rules of thumb for knowing when to use try/catch:
try/catch block.try/catch block. If not, let the program crash.try/catch block. Fix the code.undefined. That's why the optional chaining operator (?.) and nullish coalescing operator (??) were introduced... use them as needed.