mozilla

Revision 81788 of Error.prototype.toString()

  • Revision slug: JavaScript/Reference/Global_Objects/Error/toString
  • Revision title: toString
  • Revision id: 81788
  • Created:
  • Creator: fscholz
  • Is current revision? No
  • Comment remove use of mediawiki template; 20 words removed

Revision Content

 

Summary

Returns a string representing the specified Error object <table class="fullwidth-table"> <tr> <td class="header" colspan="2">Method of Error</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

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"

See also

toSource

Revision Source

<p> </p>
<h3 name="Summary">Summary</h3>
<p>Returns a string representing the specified Error object <span class="comment">&lt;table class="fullwidth-table"&gt; &lt;tr&gt; &lt;td class="header" colspan="2"&gt;Method of <a href="/en/Core_JavaScript_1.5_Reference/Global_Objects/Error" title="en/Core_JavaScript_1.5_Reference/Global_Objects/Error">Error</a>&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>
<p><code><em>error</em>.toString()</code></p>
<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" title="en/Core_JavaScript_1.5_Reference/Global_Objects/Error">Error</a> object overrides the <code><a href="/en/Core_JavaScript_1.5_Reference/Global_Objects/Object/toString" title="en/Core_JavaScript_1.5_Reference/Global_Objects/Object/toString">Object.prototype.toString</a></code> method inherited by all objects. According to <a href="/en/ECMAScript" title="en/ECMAScript">ECMA-262</a>, implementations are free to decide the behavior of this method. <a href="/en/SpiderMonkey" title="en/SpiderMonkey">SpiderMonkey</a> joins string representations of the <code><a href="/en/Core_JavaScript_1.5_Reference/Global_Objects/Error/name" title="en/Core_JavaScript_1.5_Reference/Global_Objects/Error/name">name</a></code> and <code><a href="/en/Core_JavaScript_1.5_Reference/Global_Objects/Error/message" title="en/Core_JavaScript_1.5_Reference/Global_Objects/Error/message">message</a></code> 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.</p>
<p>Note that when creating a string representation of the <code>name</code> and <code>message</code> properties, this method does not invoke those properties' <code>toString</code> 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.</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" title="en/Core_JavaScript_1.5_Reference/Global_Objects/Error/toSource">toSource</a></p>
Revert to this revision