JavaScript Error.prototype.toString() Method

The Error.prototype.toString() method is an inbuilt method in JavaScript that is used to return a string representing the specified Error object. 

Syntax:

e.toString()

Parameters: This method does not accept any parameters. 

Return value: This method returns a string representing the specified Error object. 

The below examples illustrate the Error.prototype.toString() Method in JavaScript: 

Example 1: In this example, we will try to return a string using the Error.prototype.toString() Method in JavaScript.

javascript




let Beginner1 = new Error();
console.log(Beginner1.toString());
 
Beginner1.name = undefined;
console.log(Beginner1.toString());
 
Beginner1.name = 'w3wiki';
console.log(Beginner1.toString());


Output:

Error
Error
w3wiki

Example 2: 

javascript




let Beginner = new Error('Error.prototype.toString()');
console.log(Beginner.toString());
 
Beginner.name = undefined;
console.log(Beginner.toString());
 
Beginner.name = '';
console.log(Beginner.toString());
 
Beginner.message = "Error Type";
console.log(Beginner.toString());
 
Beginner.message = undefined;
console.log(Beginner.toString());
 
Beginner.name = 'w3wiki';
console.log(Beginner.toString());


Output:

Error: Error.prototype.toString()
Error: Error.prototype.toString()
Error.prototype.toString()
Error Type

w3wiki

We have a complete list of Javascript Error Objects, to check those please go through the Javascript Error Object Complete Reference article

Supported Browsers: The browsers supported by Error.prototype.toString() Method are listed below:

  • Google Chrome
  • Firefox
  • Edge

Contact Us