Syntax

JavaScript Throw an Error Syntax

In JavaScript, throw an error: throws an error on purpose — throw new Error("Something went wrong");

Quick Answer

Syntax
throw new Error("Something went wrong");
JavaScriptSyntax

Throw an Error

Code example
throw new Error("Something went wrong");

Throws an error on purpose.

Learn More

How to throw an error in JavaScript

  1. Open your JavaScript file or editor.
  2. Copy the example, then replace its sample names and values for your code.

JavaScript Throw an Error Syntax

Use throw new Error("Something went wrong");.

Related Syntax References

Frequently asked questions

What is the JavaScript syntax for throw an error?

Use this pattern: throw new Error("Something went wrong");

Can I copy this JavaScript code example?

Yes. Use Copy code and adapt the example to your program.

Is this JavaScript syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify