Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between `throw new Error` and `throw Error`? [duplicate]

Tags:

javascript

Are there any drawbacks of throwing Errors without the new keyword?

throw new Error('Something went wrong');

/* vs */

throw Error('Something went wrong');
like image 225
Offpics Avatar asked Apr 01 '26 12:04

Offpics


1 Answers

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 expression new Error(…) with the same arguments.

like image 95
CertainPerformance Avatar answered Apr 04 '26 02:04

CertainPerformance