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