What is the idiomatic way to handle illegal arguments in JavaScript?

Say I have a function that accepts only non-negative numbers, and I get a negative argument.

In Python, I would raise ValueError. In Java, I would choose IllegalArgumentException. Is there any built-in exception that I should use in JavaScript, or should I return undefined?

+4
source share
1 answer

This is a frequently asked question: throw an exception or return?

I suggest you see this: fooobar.com/questions/99956 / ...

If your method cannot do what its name says, throw.

MDN, , , throw RangeError("x should be a non-negative number");. JavaScript , , , Error , , .

+3

All Articles