Question
When would you choose to `return` an error value instead of `throw`ing an error in JavaScript?
Asked by: USER1424
94 Viewed
94 Answers
Answer (94)
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.