What is JavaScript Error Constructor?

A JavaScript constructor is a function that creates and initializes an object instance of a class. A constructor is used to create a new object and set values for existing object properties. The Error() constructor in JavaScript is used to create new error objects. Error objects are thrown when runtime errors occur. The Error object can also be used as a base object for user-defined exceptions.

Syntax

Following is the syntax for an Error() constructor:

new Error()
new Error(message)
new Error(message, options)
new Error(message, fileName)
new Error(message, fileName, lineNumber)

Parameters

The Error() constructor can be defined with different parameters, each having its own meaning:

  • message ? Optional parameter. A human-readable description of this error object. An error message can be set using the JavaScript error message property.

  • options ? Optional parameter. A property indicating the specific reason which the error occurs. When catching and re-throwing an error with a more-specific or useful error message, this property should be used to pass the original error.

  • fileName ? Optional parameter having value for the fileName property on the created Error object. If no name is provided then fileName is equal to the name of file containing the code that calls the Error() constructor.

  • lineNumber ? Optional parameter. The value for the lineNumber property on the created Error object. If no number is provided then the lineNumber is equal to the line number containing the Error() constructor.

There are two ways to create an error object: using a function call or using the new keyword.

// Using Function Call
const x = Error('This error constructor is created using function call!');

// Using new Keyword
const y = new Error('This object is created using "new" keyword!');

Example: Creating Error using Function Call

We use Error like a function without a new keyword. When Error is used as a function, it returns an error object the same as the error object created using the new keyword. In this program, we create an error object and throw it using the throw keyword:

<html>
<body>
   <h3>Create Error Using Function Call</h3>
   <p id="result"></p>
   <script>
      const err = Error("This error is created using function call");
      try {
         throw err;
      }
      catch(e) {
         document.getElementById("result").innerHTML = e;
      }
   </script>
</body>
</html>
Error: This error is created using function call

Example: Creating Error using new Keyword

We can create an error object using the keyword 'new'. We throw the error using try...catch and throw:

<html>
<body>
   <h3>Create Error Using new Keyword</h3>
   <p id="result"></p>
   <script>
      const err = new Error("This error object is created using new keyword");
      try {
         throw err;
      }
      catch(e) {
         document.getElementById("result").innerHTML = e;
      }
   </script>
</body>
</html>
Error: This error object is created using new keyword

Example: Error Constructor with Custom Message

Here's how to create custom error messages and access error properties:

<html>
<body>
   <h3>Custom Error with Properties</h3>
   <p id="result"></p>
   <script>
      const customError = new Error("Something went wrong in the application");
      customError.name = "CustomError";
      
      try {
         throw customError;
      }
      catch(e) {
         document.getElementById("result").innerHTML = 
            "Name: " + e.name + "<br>" +
            "Message: " + e.message;
      }
   </script>
</body>
</html>
Name: CustomError
Message: Something went wrong in the application

Conclusion

The JavaScript Error constructor creates error objects that can be thrown and caught in try-catch blocks. Both function call syntax and new keyword syntax work identically for creating Error objects, making it flexible for error handling in applications.

Updated on: 2026-03-15T23:19:00+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements