Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Symbol.toStringTag

BaselineWidely available

TheSymbol.toStringTag static data property represents thewell-known symbolSymbol.toStringTag.Object.prototype.toString() looks up this symbol on thethis value for the property containing a string that represents the type of the object.

Try it

class ValidatorClass {  get [Symbol.toStringTag]() {    return "Validator";  }}console.log(Object.prototype.toString.call(new ValidatorClass()));// Expected output: "[object Validator]"

Value

The well-known symbolSymbol.toStringTag.

Property attributes ofSymbol.toStringTag
Writableno
Enumerableno
Configurableno

Examples

Default tags

Some values do not haveSymbol.toStringTag, but have specialtoString() representations. For a complete list, seeObject.prototype.toString().

js
Object.prototype.toString.call("foo"); // "[object String]"Object.prototype.toString.call([1, 2]); // "[object Array]"Object.prototype.toString.call(3); // "[object Number]"Object.prototype.toString.call(true); // "[object Boolean]"Object.prototype.toString.call(undefined); // "[object Undefined]"Object.prototype.toString.call(null); // "[object Null]"// … and more

Built-in toStringTag symbols

Most built-in objects provide their own[Symbol.toStringTag] property. Almost all built-in objects'[Symbol.toStringTag] property is not writable, not enumerable, and configurable; the exception isIterator, which is writable for compatibility reasons.

For constructor objects likePromise, the property is installed onConstructor.prototype, so that all instances of the constructor inherit[Symbol.toStringTag] and can be stringified. For non-constructor objects likeMath andJSON, the property is installed as a static property, so that the namespace object itself can be stringified. Sometimes, the constructor also provides its owntoString method (for example,Intl.Locale), in which case the[Symbol.toStringTag] property is only used when you explicitly callObject.prototype.toString on it.

js
Object.prototype.toString.call(new Map()); // "[object Map]"Object.prototype.toString.call(function* () {}); // "[object GeneratorFunction]"Object.prototype.toString.call(Promise.resolve()); // "[object Promise]"// … and more

Custom tag with toStringTag

When creating your own class, JavaScript defaults to the "Object" tag:

js
class ValidatorClass {}Object.prototype.toString.call(new ValidatorClass()); // "[object Object]"

Now, with the help oftoStringTag, you are able to set your own custom tag:

js
class ValidatorClass {  get [Symbol.toStringTag]() {    return "Validator";  }}Object.prototype.toString.call(new ValidatorClass()); // "[object Validator]"

toStringTag available on all DOM prototype objects

Due to aWebIDL spec change in mid-2020, browsers are adding aSymbol.toStringTag property to all DOM prototype objects. For example, to access theSymbol.toStringTag property onHTMLButtonElement:

js
const test = document.createElement("button");test.toString(); // "[object HTMLButtonElement]"test[Symbol.toStringTag]; // "HTMLButtonElement"

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-symbol.tostringtag

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp