mozilla

Revision 388323 of Error.prototype.toString()

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

Revision Content

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"

See also

Revision Source

<h2 id="Summary" name="Summary">Summary</h2>

<p>Returns a string representing the specified Error object.</p>

<table class="standard-table">
  <thead>
    <tr>
      <th class="header" colspan="2">Method of <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Error" title="JavaScript/Reference/Global_Objects/Error"><code>Error</code></a></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Implemented in</td>
      <td>JavaScript 1.1</td>
    </tr>
    <tr>
      <td>ECMAScript Edition</td>
      <td>ECMAScript 1st Edition</td>
    </tr>
  </tbody>
</table>

<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre><code><em>error</em>.toString()</code></pre>

<h3 id="Parameters" name="Parameters">Parameters</h3>
<p>None.</p>

<h2 id="Description" name="Description">Description</h2>

<p>The <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Error" title="JavaScript/Reference/Global_Objects/Error">Error</a> object overrides the <code><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/toString" title="JavaScript/Reference/Global_Objects/Object/toString">Object.prototype.toString</a></code> method inherited by all objects. According to <a href="/en-US/docs/ECMAScript" title="ECMAScript">ECMA-262</a>, implementations are free to decide the behavior of this method. <a href="/en-US/docs/SpiderMonkey" title="SpiderMonkey">SpiderMonkey</a> joins string representations of the <code><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Error/name" title="JavaScript/Reference/Global_Objects/Error/name">name</a></code> and <code><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Error/message" title="JavaScript/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>

<h2 id="Example" name="Example">Example</h2>
<pre class="brush:js">
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>

<h2 id="See_also" name="See_also">See also</h2>
<ul>
<li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Error/toSource" title="JavaScript/Reference/Global_Objects/Error/toSource"><code>toSource</code></a></li>
</ul>
Revert to this revision