Table of contents
- 1. Summary
- 2. Syntax
- 3. Parameters
- 4. Description
- 5. Example
- 6. See also
Summary
Returns a string representing the specified Error object.
Method of Error | |
|---|---|
| Implemented in | JavaScript 1.1 |
| ECMAScript Edition | ECMAScript 1st Edition |
Syntax
error.toString()
Parameters
None.
Description
The Error object overrides the Object.prototype.toString method inherited by all objects. According to ECMA-262, implementations are free to decide the behavior of this method. SpiderMonkey joins string representations of the name and message properties with a colon and a space separating the two. If the string representation of either of these two properties is an empty string, this method simply returns the string representation of the property that has a non-zero length. If both properties' string representations are empty strings, this method returns an empty string.
Note that when creating a string representation of the name and message properties, this method does not invoke those properties' toString methods. If the value in either of these properties is not already a string, this method will behave as if that property contained an empty string.
Example
var e = new Error("fatal error");
e.toString(); // returns "Error: fatal error"
e.name = undefined;
e.toString(); // returns "fatal error"
e.message = undefined;
e.toString(); // returns ""
e.name = "Error";
e.toString(); // returns "Error"