Error.prototype.toString()

  • Revision slug: JavaScript/Reference/Global_Objects/Error/toString
  • Revision title: toString
  • Revision id: 81784
  • Created:
  • Creator: Waldo
  • Is current revision? No
  • Comment formatting

Revision Content

Summary

Returns a string representing the specified Error object

<table class="fullwidth-table"> <tr> <td class="header" colspan="2">Method of {{mediawiki.internal('Core JavaScript 1.5 Reference:Global Objects:Error|Error', "en")}}</td> </tr> <tr> <td>Implemented in:</td> May or may not be correct <td>JavaScript 1.1, NES 2.0</td> </tr> <tr> <td>ECMA Version:</td> <td>ECMA-262</td> </tr> </table>

Syntax

var errorString = error.toString();

Parameters

None.

Description

The Error object overrides the Object.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 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.

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"

See also

toSource

Revision Source

<p>
</p>
<h3 name="Summary"> Summary </h3>
<p>Returns a string representing the specified Error object
</p><p><span class="comment">&lt;table class="fullwidth-table"&gt; &lt;tr&gt; &lt;td class="header" colspan="2"&gt;Method of {{mediawiki.internal('Core JavaScript 1.5 Reference:Global Objects:Error|Error', "en")}}&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Implemented in:&lt;/td&gt;   May or may not be correct &lt;td&gt;JavaScript 1.1, NES 2.0&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;ECMA Version:&lt;/td&gt; &lt;td&gt;ECMA-262&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt;</span>
</p>
<h3 name="Syntax"> Syntax </h3>
<pre class="eval">var errorString = <i>error</i>.toString();
</pre>
<h3 name="Parameters"> Parameters </h3>
<p>None.
</p>
<h3 name="Description"> Description </h3>
<p>The <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Error">Error</a> object overrides the <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Object/toString">Object.toString</a> method inherited by all objects. According to <a href="en/ECMA-262">ECMA-262</a>, implementations are free to decide the behavior of this method. <a href="en/SpiderMonkey">SpiderMonkey</a> joins string representations of the <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Error/name">name</a> and <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/message">message</a> properties with a colon 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.
</p>
<h3 name="Example"> Example </h3>
<pre class="eval">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"
</pre>
<h3 name="See_also"> See also </h3>
<p><a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Error/toSource">toSource</a>
</p>
Revert to this revision