mozilla

Revision 509845 of String.prototype.toString()

  • Revision slug: Web/JavaScript/Reference/Global_Objects/String/toString
  • Revision title: String.prototype.toString()
  • Revision id: 509845
  • Created:
  • Creator: fscholz
  • Is current revision? No
  • Comment

Revision Content

{{JSRef("Global_Objects", "String")}}

Summary

The toString() method returns a string representing the specified object.

Syntax

str.toString()

Description

The String object overrides the toString method of the {{jsxref("Global_Objects/Object", "Object")}} object; it does not inherit {{jsxref("Object.toString()")}}. For String objects, the toString method returns a string representation of the object and is the same as the {{jsxref("String.prototype.valueOf()")}} method.

Examples

Example: Using toString

The following example displays the string value of a String object:

var x = new String("Hello world");

alert(x.toString())      // Displays "Hello world"

Specifications

Specification Status Comment
ECMAScript 1st Edition. Standard Initial definition.
Implemented in JavaScript 1.1.
{{SpecName('ES5.1', '#sec-15.5.4.2', 'String.prototype.toString')}} {{Spec2('ES5.1')}}  
{{SpecName('ES6', '#sec-string.prototype.tostring', 'String.prototype.toString')}} {{Spec2('ES6')}}  

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}

See also

  • {{jsxref("Object.prototype.toSource()")}}
  • {{jsxref("String.prototype.valueOf()")}}

Revision Source

<div>
 {{JSRef("Global_Objects", "String")}}</div>
<h2 id="Summary" name="Summary">Summary</h2>
<p>The <code><strong>toString()</strong></code> method returns a string representing the specified object.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<code><em>str</em>.toString()</code></pre>
<h2 id="Description" name="Description">Description</h2>
<p>The <code>String</code> object overrides the <code>toString</code> method of the {{jsxref("Global_Objects/Object", "Object")}} object; it does not inherit {{jsxref("Object.toString()")}}. For <code>String</code> objects, the <code>toString</code> method returns a string representation of the object and is the same as the {{jsxref("String.prototype.valueOf()")}} method.</p>
<h2 id="Examples" name="Examples">Examples</h2>
<h3 id="Example:_Using_toString" name="Example:_Using_toString">Example: Using <code>toString</code></h3>
<p>The following example displays the string value of a String object:</p>
<pre class="brush:js">
var x = new String("Hello world");

alert(x.toString())      // Displays "Hello world"</pre>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>ECMAScript 1st Edition.</td>
   <td>Standard</td>
   <td>Initial definition.<br />
    Implemented in JavaScript 1.1.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-15.5.4.2', 'String.prototype.toString')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-string.prototype.tostring', 'String.prototype.toString')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Chrome</th>
    <th>Firefox (Gecko)</th>
    <th>Internet Explorer</th>
    <th>Opera</th>
    <th>Safari</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
   </tr>
  </tbody>
 </table>
</div>
<div id="compat-mobile">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Android</th>
    <th>Chrome for Android</th>
    <th>Firefox Mobile (Gecko)</th>
    <th>IE Mobile</th>
    <th>Opera Mobile</th>
    <th>Safari Mobile</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
   </tr>
  </tbody>
 </table>
</div>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
 <li>{{jsxref("Object.prototype.toSource()")}}</li>
 <li>{{jsxref("String.prototype.valueOf()")}}</li>
</ul>
Revert to this revision