Object.prototype.toString()

  • Revision slug: JavaScript/Reference/Global_Objects/Object/toString
  • Revision title: toString
  • Revision id: 61282
  • Created:
  • Creator: Maian
  • Is current revision? No
  • Comment /* Summary */ "specified" makes no sense here

Revision Content

Summary

Returns a string representing the object.

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

Syntax

toString()

Description

Every object has a toString method that is automatically called when it is to be represented as a text value or when an object is referred to in a string concatenation. For example, the following examples require theDog to be represented as a string:

document.write(theDog)
document.write("The dog is " + theDog)

By default, the toString method is inherited by every object descended from Object. You can override this method for custom objects that you create. If you do not override toString in a custom object, toString returns {{mediawiki.external('object <i>type</i>')}}, where type is the object type or the name of the constructor function that created the object.

For example:

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

Built-in toString methods

Every built-in core JavaScript object overrides the toString method of Object to return an appropriate value. JavaScript calls this method whenever it needs to convert an object to a string.

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","girl")

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 dogToString, the function that will be used to override the default toString method. This function generates a string containing each property, of the form "property = value;".

function dogToString() {
   var ret = "Dog " + this.name + " is [\n"
   for (var prop in this)
      ret += " " + prop + " is " + this[prop] + ";\n"
   return ret + "]"
}

The following code assigns the user-defined function to the object's toString method:

Dog.prototype.toString = dogToString

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 [
  name is Gabby;
  breed is Lab;
  color is chocolate;
  sex is girl;
]

An object's toString method is usually invoked by JavaScript, but you can invoke it yourself as follows:

var dogString = theDog.toString()

Backward Compatibility

JavaScript 1.2

The behavior of the toString method depends on whether you specify LANGUAGE="JavaScript1.2" in the <SCRIPT> tag:

  • If you specify LANGUAGE="JavaScript1.2" in the <SCRIPT> tag, the toString method returns an object literal.
  • If you do not specify LANGUAGE="JavaScript1.2" in the <SCRIPT> tag, the toString method returns {{mediawiki.external('object <i>type</i>')}}, as with other JavaScript versions.

Examples

Example: The location object

The following example prints the string equivalent of the current location.

document.write("location.toString() is " + location.toString() + "<BR>")

The output is as follows:

location.toString() is file:///C|/TEMP/myprog.htmll

Example: Object with no string value

Assume you have an Image object named sealife defined as follows:

<IMG NAME="sealife" SRC="images\seaotter.gif" ALIGN="left" VSPACE="10">

Because the Image object itself has no special toString method, sealife.toString() returns the following:

[object Image]

Example: The radix parameter

The following example prints the string equivalents of the numbers 0 through 9 in decimal and binary.

for (x = 0; x < 10; x++) {
   document.write("Decimal: ", x.toString(10), " Binary: ",
      x.toString(2), "<BR>")
}

The preceding example produces the following output:

Decimal: 0 Binary: 0
Decimal: 1 Binary: 1
Decimal: 2 Binary: 10
Decimal: 3 Binary: 11
Decimal: 4 Binary: 100
Decimal: 5 Binary: 101
Decimal: 6 Binary: 110
Decimal: 7 Binary: 111
Decimal: 8 Binary: 1000
Decimal: 9 Binary: 1001

See Also

toSource, valueOf

{{ wiki.languages( { "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/Object" } ) }}

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/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>
toString()
</code>
</p>
<h3 name="Description"> Description </h3>
<p>Every object has a <code>toString</code> method that is automatically called when it is to be represented as a text value or when an object is referred to in a string concatenation. For example, the following examples require <code>theDog</code> to be represented as a string:
</p>
<pre>document.write(theDog)
document.write("The dog is " + theDog)
</pre>
<p>By default, the <code>toString</code> method is inherited by every object descended from <code>Object</code>. You can override this method for custom objects that you create. If you do not override <code>toString</code> 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 or the name of the constructor function that created the object.
</p><p>For example:
</p>
<pre>var o = new Object()
o.toString // returns [object Object]
</pre>
<h4 name="Built-in_toString_methods"> Built-in <code>toString</code> methods </h4>
<p>Every built-in core JavaScript object overrides the <code>toString</code> method of <code>Object</code> to return an appropriate value. JavaScript calls this method whenever it needs to convert an object to a string.
</p>
<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>function Dog(name,breed,color,sex) {
   this.name=name
   this.breed=breed
   this.color=color
   this.sex=sex
}

theDog = new Dog("Gabby","Lab","chocolate","girl")
</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>theDog.toString() //returns [object Object]
</pre>
<p>The following code creates <code>dogToString</code>, the function that will be used to override the default <code>toString</code> method. This function generates a string containing each property, of the form "<code>property = value;</code>".
</p>
<pre>function dogToString() {
   var ret = "Dog " + this.name + " is [\n"
   for (var prop in this)
      ret += " " + prop + " is " + this[prop] + ";\n"
   return ret + "]"
}
</pre>
<p>The following code assigns the user-defined function to the object's <code>toString</code> method:
</p>
<pre>Dog.prototype.toString = dogToString
</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>Dog Gabby is [
  name is Gabby;
  breed is Lab;
  color is chocolate;
  sex is girl;
]
</pre>
<p>An object's <code>toString</code> method is usually invoked by JavaScript, but you can invoke it yourself as follows:
</p>
<pre>var dogString = theDog.toString()
</pre>
<h3 name="Backward_Compatibility"> Backward Compatibility </h3>
<h4 name="JavaScript_1.2"> JavaScript 1.2 </h4>
<p>The behavior of the <code>toString</code> method depends on whether you specify <code>LANGUAGE="JavaScript1.2"</code> in the <code>&lt;SCRIPT&gt;</code> tag:
</p>
<ul><li> If you specify <code>LANGUAGE="JavaScript1.2"</code> in the <code>&lt;SCRIPT&gt;</code> tag, the <code>toString</code> method returns an object literal.
</li></ul>
<ul><li> If you do not specify <code>LANGUAGE="JavaScript1.2"</code> in the <code>&lt;SCRIPT&gt;</code> tag, the <code>toString</code> method returns <code>{{mediawiki.external('object &lt;i&gt;type&lt;/i&gt;')}}</code>, as with other JavaScript versions.
</li></ul>
<h3 name="Examples"> Examples </h3>
<h4 name="Example:_The_location_object"> Example: The location object </h4>
<p>The following example prints the string equivalent of the current location.
</p>
<pre>document.write("location.toString() is " + location.toString() + "&lt;BR&gt;")
</pre>
<p>The output is as follows:
</p>
<pre>location.toString() is file:///C|/TEMP/myprog.htmll
</pre>
<h4 name="Example:_Object_with_no_string_value"> Example: Object with no string value </h4>
<p>Assume you have an <code>Image</code> object named <code>sealife</code> defined as follows:
</p>
<pre>&lt;IMG NAME="sealife" SRC="images\seaotter.gif" ALIGN="left" VSPACE="10"&gt;
</pre>
<p>Because the <code>Image</code> object itself has no special <code>toString</code> method, <code>sealife.toString()</code> returns the following:
</p>
<pre>[object Image]
</pre>
<h4 name="Example:_The_radix_parameter"> Example: The radix parameter </h4>
<p>The following example prints the string equivalents of the numbers 0 through 9 in decimal and binary.
</p>
<pre>for (x = 0; x &lt; 10; x++) {
   document.write("Decimal: ", x.toString(10), " Binary: ",
      x.toString(2), "&lt;BR&gt;")
}
</pre>
<p>The preceding example produces the following output:
</p>
<pre>Decimal: 0 Binary: 0
Decimal: 1 Binary: 1
Decimal: 2 Binary: 10
Decimal: 3 Binary: 11
Decimal: 4 Binary: 100
Decimal: 5 Binary: 101
Decimal: 6 Binary: 110
Decimal: 7 Binary: 111
Decimal: 8 Binary: 1000
Decimal: 9 Binary: 1001
</pre>
<h3 name="See_Also"> See Also </h3>
<p><a href="en/Core_JavaScript_1.5_Reference/Objects/Object/toSource">toSource</a>,
<a href="en/Core_JavaScript_1.5_Reference/Objects/Object/valueOf">valueOf</a>
</p>{{ wiki.languages( { "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/Object" } ) }}
Revert to this revision