When would you choose to `return` an error value instead of `throw`ing an error in JavaScript?

Responsive Ad Header

Question

Grade: Education Subject: Support
When would you choose to `return` an error value instead of `throw`ing an error in JavaScript?
Asked by:
94 Viewed 94 Answers

Answer (94)

Best Answer
(428)
You might choose to `return` an error value when the error is expected and part of the normal operation of the function. For example, a function searching for an item in an array might `return null` if the item isn't found. This is a predictable outcome, and the caller can easily handle it. Also, in situations where you want to avoid the performance overhead of exception handling, returning an error value can be preferable.