Symbol.toStringTag
The Symbol.toStringTag well-known symbol is a string valued property that is used in the creation of the default string description of an object. It is accessed internally by the Object.prototype.toString() method.
Try it

   Property attributes of Symbol.toStringTag
     Writable no   Enumerable no   Configurable no   
Examples
Default tags
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
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 classes default to object tag

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

Object.prototype.toString.call(new ValidatorClass()); // "[object Object]"

Custom tag with toStringTag

Now, with the help of toStringTag, you are able to set your own custom tag: class ValidatorClass {
  get [Symbol.toStringTag]() {
    return 'Validator';
  }
}

Object.prototype.toString.call(new ValidatorClass()); // "[object Validator]"

toStringTag available on all DOM prototype objects

Due to a WebIDL spec change in mid-2020, browsers are adding a Symbol.toStringTag property to all DOM prototype objects. For example, to access the Symbol.toStringTag property on HTMLButtonElement: const test = document.createElement('button');
test.toString(); // Returns [object HTMLButtonElement]
test[Symbol.toStringTag];  // Returns HTMLButtonElement

Specifications

Specification
ECMAScript Language Specification # sec-symbol.tostringtag

Browser compatibility




Desktop
Mobile
Server



Chrome
Edge
Firefox
Internet Explorer
Opera
Safari
WebView Android
Chrome Android
Firefox for Android
Opera Android
Safari on IOS
Samsung Internet
Deno
Node.js




toStringTag
49
15
51
No
36
10
49
49
51
36
10
5.0
1.0

6.0.0
4.0.0



dom_objects
50
79
78
No
37
14
50
50
79
37
14
5.0
1.0
No



See also
 Polyfill of Symbol.toStringTag in core-js Object.prototype.toString() 

Found a problem with this page?

Edit on GitHub
Source on GitHub
Report a problem with this content on GitHub
Want to fix the problem yourself? See our Contribution guide.


Last modified: Aug 5, 2022, by MDN contributors


    
{'path': 'public/docs/javascript/global_objects/symbol/tostringtag.html', 'source': 'DevDocs.IO'}