JavaScript | Error.prototype.toString() Method

The Error.prototype.toString() method is an inbuilt method in JavaScript which is used to returns 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.

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



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

var geeks1 = new Error();
document.writeln(geeks1.toString()); 
document.writeln("<br>");
   
geeks1.name = undefined;
document.writeln(geeks1.toString()); 
document.writeln("<br>");
  
geeks1.name = 'GeeksForGeeks';
document.writeln(geeks1.toString());

chevron_right


Output:

Error
Error
GeeksForGeeks

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

var geeks = new Error('Error.prototype.toString()');
document.writeln(geeks.toString()); 
document.writeln("<br>");
   
geeks.name = undefined;
document.writeln(geeks.toString()); 
document.writeln("<br>");
   
geeks.name = '';
document.writeln(geeks.toString()); 
document.writeln("<br>");
   
geeks.message = "Error Type";
document.writeln(geeks.toString());
document.writeln("<br>");
   
geeks.message = undefined;
document.writeln(geeks.toString());
document.writeln("<br>");
   
geeks.name = 'GeeksForGeeks';
document.writeln(geeks.toString());

chevron_right


Output:

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

GeeksForGeeks

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

  • Google Chrome
  • Firefox
  • Edge

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.