Object.prototype.toString()

  • Revision slug: Web/JavaScript/Reference/Global_Objects/Object/toString
  • Revision title: Object.toString
  • Revision id: 417963
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment Moved From JavaScript/Reference/Global_Objects/Object/toString to Web/JavaScript/Reference/Global_Objects/Object/toString

Revision Content

Summary

Returns a string representing the object.

Method of Object
Implemented in JavaScript 1.0
ECMAScript Edition ECMAScript 1st Edition

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 "[object type]", where type is the object type. The following code illustrates this:

var o = new Object();
o.toString();           // returns [object Object]
Starting in JavaScript 1.8.5 toString() called on null returns [object Null], and undefined returns [object Undefined], as defined in the 5th Edition of ECMAScript and a subsequent Errata. See Using toString to detect object type.

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 a female chocolate Lab

Using toString() to detect object class

toString() can be used with every object and allows you to get its class. To use the Object.prototype.toString() with every object, you need to call Function.prototype.call() or Function.prototype.apply() on it, passing the object you want to inspect as the first parameter called thisArg.

var toString = Object.prototype.toString;

toString.call(new Date); // [object Date]
toString.call(new String); // [object String]
toString.call(Math); // [object Math]

//Since JavaScript 1.8.5
toString.call(undefined); // [object Undefined]
toString.call(null); // [object Null]

See also

Revision Source

<h2 id="Summary" name="Summary">Summary</h2>
<p>Returns a string representing the object.</p>
<table class="standard-table">
  <thead>
    <tr>
      <th class="header" colspan="2">Method of <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object" title="JavaScript/Reference/Global_Objects/Object"><code>Object</code></a></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Implemented in</td>
      <td>JavaScript 1.0</td>
    </tr>
    <tr>
      <td>ECMAScript Edition</td>
      <td>ECMAScript 1st Edition</td>
    </tr>
  </tbody>
</table>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<code><em>object</em>.toString()</code></pre>
<h2 id="Description" name="Description">Description</h2>
<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 "[object <em>type</em>]", where <code><em>type</em></code> is the object type. The following code illustrates this:</p>
<pre class="brush:js">
var o = new Object();
o.toString();           // returns [object Object]</pre>
<div class="note">
  Starting in JavaScript 1.8.5 <code>toString()</code> called on <code>null</code> returns <code>[object <em>Null</em>]</code>, and <code>undefined</code> returns <code>[object <em>Undefined</em>]</code>, as defined in the 5th Edition of ECMAScript and a subsequent Errata. See <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/toString#Using_toString_to_detect_object_type" title="JavaScript/Reference/Global Objects/Object/toString#Using toString to detect object type">Using toString to detect object type</a>.</div>
<h2 id="Examples" name="Examples">Examples</h2>
<h3 id="Overriding_the_default_toString_method" name="Overriding_the_default_toString_method">Overriding the default <code>toString</code> method</h3>
<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="brush: js">
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>
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="brush: js">
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>
Dog Gabby is a female chocolate Lab</pre>
<h3 id="Using_toString_to_detect_object_type" name="Using_toString_to_detect_object_type">Using toString() to detect object class</h3>
<p><code>toString()</code> can be used with every object and allows you to get its class. To use the <code>Object.prototype.toString()</code> with every object, you need to call <code>Function.prototype.call()</code> or <code>Function.prototype.apply()</code> on it, passing the object you want to inspect as the first parameter called <code>thisArg</code>.</p>
<pre class="brush: js">
var toString = Object.prototype.toString;

toString.call(new Date); // [object Date]
toString.call(new String); // [object String]
toString.call(Math); // [object Math]

//Since JavaScript 1.8.5
toString.call(undefined); // [object Undefined]
toString.call(null); // [object Null]
</pre>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/toSource" title="JavaScript/Reference/Global_Objects/Object/toSource">Object.toSource</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/valueOf" title="JavaScript/Reference/Global_Objects/Object/valueOf">Object.valueOf</a></li>
</ul>
Revert to this revision