Object.prototype.toString()

  • Revision slug: JavaScript/Reference/Global_Objects/Object/toString
  • Revision title: toString
  • Revision id: 61294
  • Created:
  • Creator: Sevenspade
  • Is current revision? No
  • Comment bypassing redirect

Revision Content

Summary

Returns a string representing the object.

Method of Object
Implemented in: JavaScript 1.0
ECMA Version: ECMA-262

Syntax

object.toString()

Description

Every object has a toString method that is automatically called when the object is to be represented as a text value or when an object is referred to in a manner in which a string is expected. By default, the toString method is inherited by every object descended from Object. If this method is not overridden in a custom object, toString returns {{mediawiki.external('object <i>type</i>')}}, where type is the object type. The following code illustrates this:

var o = new Object();
o.toString();           // returns [object Object]

Built-in toString methods

Each core JavaScript object has its own unique toString method to return an appropriate value when JavaScript needs to convert that object into a string. These include: {{wiki.template(':en/ToString')}}

Examples

Overriding the default toString method

You can create a function to be called in place of the default toString method. The toString method takes no arguments and should return a string. The toString method you create can be any value you want, but it will be most useful if it carries information about the object.

The following code defines the Dog object type and creates theDog, an object of type Dog:

function Dog(name,breed,color,sex) {
   this.name=name;
   this.breed=breed;
   this.color=color;
   this.sex=sex;
}

theDog = new Dog("Gabby","Lab","chocolate","female");

If you call the toString method on this custom object, it returns the default value inherited from Object:

theDog.toString(); //returns [object Object]

The following code creates and assigns dogToString to override the default toString method. This function generates a string containing the name, breed, color, and sex of the object, in the form "property = value;".

Dog.prototype.toString = function dogToString() {
  var ret = "Dog " + this.name + " is a " + this.sex + " " + this.color + " " + this.breed;
  return ret;
}

With the preceding code in place, any time theDog is used in a string context, JavaScript automatically calls the dogToString function, which returns the following string:

Dog Gabby is female chocolate Lab

See Also

{{ wiki.languages( { "es": "es/Referencia_de_JavaScript_1.5/Objetos_globales/Object/toString", "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Objets_globaux/Object/toString", "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/Object/toString", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/Object/toString" } ) }}

Revision Source

<p>
</p>
<h3 name="Summary"> Summary </h3>
<p>Returns a string representing the object.
</p>
<table class="fullwidth-table">
<tbody><tr>
<td class="header" colspan="2">Method of <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Object">Object</a></td>
</tr>
<tr>
<td>Implemented in:</td>
<td>JavaScript 1.0</td>
</tr>
<tr>
<td>ECMA Version:</td>
<td>ECMA-262</td>
</tr>
</tbody></table>
<h3 name="Syntax"> Syntax </h3>
<p><code><i>object</i>.toString()</code>
</p>
<h3 name="Description"> Description </h3>
<p>Every object has a <code>toString</code> method that is automatically called when the object is to be represented as a text value or when an object is referred to in a manner in which a string is expected. By default, the <code>toString</code> method is inherited by every object descended from <code>Object</code>. If this method is not overridden in a custom object, <code>toString</code> returns <code>{{mediawiki.external('object &lt;i&gt;type&lt;/i&gt;')}}</code>, where <code><i>type</i></code> is the object type. The following code illustrates this:
</p>
<pre class="eval">var o = new Object();
o.toString();           // returns [object Object]
</pre>
<h4 name="Built-in_toString_methods"> Built-in <code>toString</code> methods </h4>
<p>Each core JavaScript object has its own unique <code>toString</code> method to return an appropriate value when JavaScript needs to convert that object into a string. These include: {{wiki.template(':en/ToString')}}
</p>
<h3 name="Examples"> Examples </h3>
<h4 name="Overriding_the_default_toString_method"> Overriding the default <code>toString</code> method </h4>
<p>You can create a function to be called in place of the default <code>toString</code> method. The <code>toString</code> method takes no arguments and should return a string. The <code>toString</code> method you create can be any value you want, but it will be most useful if it carries information about the object.
</p><p>The following code defines the <code>Dog</code> object type and creates <code>theDog</code>, an object of type <code>Dog</code>:
</p>
<pre class="eval">function Dog(name,breed,color,sex) {
   this.name=name;
   this.breed=breed;
   this.color=color;
   this.sex=sex;
}

theDog = new Dog("Gabby","Lab","chocolate","female");
</pre>
<p>If you call the <code>toString</code> method on this custom object, it returns the default value inherited from <code>Object</code>:
</p>
<pre class="eval">theDog.toString(); //returns [object Object]
</pre>
<p>The following code creates and assigns <code>dogToString</code> to override the default <code>toString</code> method. This function generates a string containing the name, breed, color, and sex of the object, in the form "<code>property = value;</code>".
</p>
<pre class="eval">Dog.prototype.toString = function dogToString() {
  var ret = "Dog " + this.name + " is a " + this.sex + " " + this.color + " " + this.breed;
  return ret;
}
</pre>
<p>With the preceding code in place, any time <code>theDog</code> is used in a string context, JavaScript automatically calls the <code>dogToString</code> function, which returns the following string:
</p>
<pre class="eval">Dog Gabby is female chocolate Lab
</pre>
<h3 name="See_Also"> See Also </h3>
<ul><li><a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Object/toSource">toSource</a>
</li><li><a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Object/valueOf">valueOf</a>
</li></ul>
<div class="noinclude">
</div>
{{ wiki.languages( { "es": "es/Referencia_de_JavaScript_1.5/Objetos_globales/Object/toString", "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Objets_globaux/Object/toString", "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/Object/toString", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/Object/toString" } ) }}
Revert to this revision