Skip to content

Files

Latest commit

 

History

History
23 lines (17 loc) · 519 Bytes

throw-new-error.md

File metadata and controls

23 lines (17 loc) · 519 Bytes

Pattern: Error constructor without new keyword

Issue: -

Description

While errors can be created without the new keyword, using it makes the constructor call explicit and clearer. Always use new when throwing error objects.

Examples

Example of incorrect code:

throw Error("🦄");
throw TypeError("unicorn");
throw lib.TypeError("unicorn");

Example of correct code:

throw new Error("🦄");
throw new TypeError("unicorn");
throw new lib.TypeError("unicorn");