Error.prototype.toString() Method in JavaScript

JavaScript's Error.prototype.toString() method is a built-in method that converts error objects to readable string representations. This method is essential for error handling, logging, and debugging in JavaScript applications.

Error.prototype.toString() Method

The Error.prototype.toString() method converts an error object to a string format. It returns a string containing the error name, followed by a colon and space, then the error message. For example, a standard Error object will produce a string like "Error: something went wrong".

Syntax

errorObject.toString()

Parameters: None

Return Value: A string representation of the error object

Basic Usage Example

Here's a simple example showing how toString() converts an error to a string:

<html>
<body>
   <p id="output"></p>
   <script>
      try {
         throw new Error('Something went wrong');
      } catch (error) {
         document.getElementById("output").innerHTML = error.toString();
      }
   </script>
</body>
</html>

In this example, we create an Error object and use the toString() method to convert it to a readable string format for display.

Custom Error Types

The toString() method works with custom error classes, allowing you to create more specific error messages:

<html>
<body>
   <p id="custom-output"></p>
   <script>
      class ValidationError extends Error {
         constructor(message) {
            super(message);
            this.name = 'ValidationError';
         }
      }
      
      try {
         throw new ValidationError('Invalid email format');
      } catch (error) {
         document.getElementById("custom-output").innerHTML = error.toString();
         // Output: "ValidationError: Invalid email format"
      }
   </script>
</body>
</html>

This example creates a custom ValidationError class. When converted to string, it displays the custom error name instead of the generic "Error".

Comparison with Other Error Methods

Method Output Use Case
error.toString() Error: message User-friendly display
error.message message only Just the error message
error.name Error type only Error classification

Practical Example: Error Logging

<html>
<body>
   <p id="log-output"></p>
   <script>
      function processData(data) {
         try {
            if (!data) {
               throw new Error('Data is required');
            }
            if (typeof data !== 'object') {
               throw new TypeError('Data must be an object');
            }
            return "Data processed successfully";
         } catch (error) {
            const errorLog = `[${new Date().toISOString()}] ${error.toString()}`;
            document.getElementById("log-output").innerHTML = errorLog;
            return null;
         }
      }
      
      // Test with invalid data
      processData(null);
   </script>
</body>
</html>

This example demonstrates using toString() for error logging with timestamps, making it easier to track and debug issues.

Key Points

  • The toString() method combines error name and message into a readable format
  • Works with both built-in and custom error types
  • Useful for logging, debugging, and user-friendly error displays
  • Returns a string in the format "ErrorName: error message"

Conclusion

The Error.prototype.toString() method is essential for converting error objects to readable strings. It's particularly useful for error logging, debugging, and creating user-friendly error messages in JavaScript applications.

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

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements