Try-catch recommended instead of type checking in javascript?

In python, I often heard that instead of checking the type of a variable to determine whether you want to perform a specific operation on it, you should simply wrap the operation in a Try statement and handle the exception if you have the wrong input type.

Does the same apply to javascript? that is, should the preferred try / catch approach be used instead of typeof?

+6
source share
1 answer

However, the relief is handling exceptions in any language, the rule is to always use exceptions to check for exceptional ones.

What do I mean, if your algorithm needs type checking during normal operation, then it is better to check typing explicitly using the condition, if type checking is performed to detect abnormal work, it is better to raise / handle an exception.

Although, since Javascript is free to type in, most typing problems won't throw an exception, but instead work in a way that you don't always expect ...

+3
source

Source: https://habr.com/ru/post/969589/


All Articles