Open In App

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.




let geeks1 = new Error();
console.log(geeks1.toString());
 
geeks1.name = undefined;
console.log(geeks1.toString());
 
geeks1.name = 'GeeksForGeeks';
console.log(geeks1.toString());

Output:

Error
Error
GeeksForGeeks

Example 2: 




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

Output:

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

GeeksForGeeks

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:

Article Tags :