Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Object.prototype.toString()

BaselineWidely available

ThetoString() method ofObject instances returns a string representing this object. This method is meant to be overridden by derived objects for customtype coercion logic.

Try it

const map = new Map();console.log(map.toString());// Expected output: "[object Map]"

Syntax

js
toString()

Parameters

By defaulttoString() takes no parameters. However, objects that inherit fromObject may override it with their own implementations that do take parameters. For example, theNumber.prototype.toString() andBigInt.prototype.toString() methods take an optionalradix parameter.

Return value

A string representing the object.

Description

JavaScript calls thetoString method toconvert an object to a primitive value. You rarely need to invoke thetoString method yourself; JavaScript automatically invokes it when encountering an object where a primitive value is expected.

This method is called in priority bystring conversion, butnumeric conversion andprimitive conversion callvalueOf() in priority. However, because the basevalueOf() method returns an object, thetoString() method is usually called in the end, unless the object overridesvalueOf(). For example,+[1] returns1, because itstoString() method returns"1", which is then converted to a number.

All objects that inherit fromObject.prototype (that is, all exceptnull-prototype objects) inherit thetoString() method. When you create a custom object, you can overridetoString() to call a custom method, so that your custom object can be converted to a string value. Alternatively, you can add a[Symbol.toPrimitive]() method, which allows even more control over the conversion process, and will always be preferred overvalueOf ortoString for any type conversion.

To use the baseObject.prototype.toString() with an object that has it overridden (or to invoke it onnull orundefined), you need to callFunction.prototype.call() orFunction.prototype.apply() on it, passing the object you want to inspect as the first parameter (calledthisArg).

js
const arr = [1, 2, 3];arr.toString(); // "1,2,3"Object.prototype.toString.call(arr); // "[object Array]"

Object.prototype.toString() returns"[object Type]", whereType is the object type. If the object has aSymbol.toStringTag property whose value is a string, that value will be used as theType. Many built-in objects, includingMap andSymbol, have aSymbol.toStringTag. Some objects predating ES6 do not haveSymbol.toStringTag, but have a special tag nonetheless. They include (the tag is the same as the type name given below):

Thearguments object returns"[object Arguments]". Everything else, including user-defined classes, unless with a customSymbol.toStringTag, will return"[object Object]".

Object.prototype.toString() invoked onnull andundefined returns[object Null] and[object Undefined], respectively.

Examples

Overriding toString for custom objects

You can create a function to be called in place of the defaulttoString() method. ThetoString() function you create should return a string value. If it returns an object and the method is called implicitly duringtype conversion, then its result is ignored and the value of a related method,valueOf(), is used instead, or aTypeError is thrown if none of these methods return a primitive.

The following code defines aDog class.

js
class Dog {  constructor(name, breed, color, sex) {    this.name = name;    this.breed = breed;    this.color = color;    this.sex = sex;  }}

If you call thetoString() method, either explicitly or implicitly, on an instance ofDog, it returns the default value inherited fromObject:

js
const theDog = new Dog("Gabby", "Lab", "chocolate", "female");theDog.toString(); // "[object Object]"`${theDog}`; // "[object Object]"

The following code overrides the defaulttoString() method. This method generates a string containing thename,breed,color, andsex of the object.

js
class Dog {  constructor(name, breed, color, sex) {    this.name = name;    this.breed = breed;    this.color = color;    this.sex = sex;  }  toString() {    return `Dog ${this.name} is a ${this.sex} ${this.color} ${this.breed}`;  }}

With the preceding code in place, any time an instance ofDog is used in a string context, JavaScript automatically calls thetoString() method.

js
const theDog = new Dog("Gabby", "Lab", "chocolate", "female");`${theDog}`; // "Dog Gabby is a female chocolate Lab"

Using toString() to detect object class

toString() can be used with every object and (by default) allows you to get its class.

js
const toString = Object.prototype.toString;toString.call(new Date()); // [object Date]toString.call(new String()); // [object String]// Math has its Symbol.toStringTagtoString.call(Math); // [object Math]toString.call(undefined); // [object Undefined]toString.call(null); // [object Null]

UsingtoString() in this way is unreliable; objects can change the behavior ofObject.prototype.toString() by defining aSymbol.toStringTag property, leading to unexpected results. For example:

js
const myDate = new Date();Object.prototype.toString.call(myDate); // [object Date]myDate[Symbol.toStringTag] = "myDate";Object.prototype.toString.call(myDate); // [object myDate]Date.prototype[Symbol.toStringTag] = "prototype polluted";Object.prototype.toString.call(new Date()); // [object prototype polluted]

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-object.prototype.tostring

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp