When is `new Error ()` better than `Error ()`?

The ES5 language specification clearly indicates that Error(foo) does the same as new Error(foo) .

But I notice that in the wild, the longer form new Error(foo) much more common.

Is there a reason for this?

Is there a situation where using new Error(foo) preferable to using Error(foo) ?

+8
javascript idiomatic error-handling
Aug 04 '16 at 5:53 on
source share
1 answer

Is there a reason for this?

It's just a habit to always call constructors with new . Rules of consistency!

This is good practice, even if they work without new , and is recommended by several style guides and related tools. Btw, since ES6 Error is a subclass , and subclasses require new .

+10
Aug 04 '16 at 7:13
source share



All Articles