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 zambiatek1 = new Error();console.log(zambiatek1.toString());zambiatek1.name = undefined;console.log(zambiatek1.toString());zambiatek1.name = 'GeeksForGeeks';console.log(zambiatek1.toString()); |
Output:
Error Error GeeksForGeeks
Example 2:
javascript
let zambiatek = new Error('Error.prototype.toString()');console.log(zambiatek.toString());zambiatek.name = undefined;console.log(zambiatek.toString());zambiatek.name = '';console.log(zambiatek.toString());zambiatek.message = "Error Type";console.log(zambiatek.toString());zambiatek.message = undefined;console.log(zambiatek.toString());zambiatek.name = 'GeeksForGeeks';console.log(zambiatek.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:
- Google Chrome
- Firefox
- Edge



