Are there any drawbacks of throwing Errors without the new
keyword?
throw new Error('Something went wrong');
/* vs */
throw Error('Something went wrong');
Are there any drawbacks of throwing Errors without the new
keyword?
throw new Error('Something went wrong');
/* vs */
throw Error('Something went wrong');
They are exactly the same, as guaranteed by the specification:
19.5.1 The Error Constructor
The Error constructor:
...
creates and initializes a new Error object when called as a function rather than as a constructor. Thus the function call
Error(…)
is equivalent to the object creation expressionnew Error(…)
with the same arguments.