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.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
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 acccess the Symbol.toStringTag
property on HTMLButtonElement
:
let test = document.createElement('button'); test.toString(); // Returns [object HTMLButtonElement] test[Symbol.toStringTag]; // Returns HTMLButtonElement
Specifications
Specification |
---|
ECMAScript (ECMA-262) The definition of 'Symbol.toStringTag' in that specification. |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Desktop | Mobile | Server | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
toStringTag | Chrome Full support 49 | Edge Full support 15 | Firefox Full support 51 | IE No support No | Opera Full support 36 | Safari Full support 10 | WebView Android Full support 49 | Chrome Android Full support 49 | Firefox Android Full support 51 | Opera Android Full support 36 | Safari iOS Full support 10 | Samsung Internet Android Full support 5.0 | nodejs
Full support
6.0.0
|
toStringTag available on all DOM prototype objects | Chrome Full support 50 | Edge Full support 79 | Firefox Full support 78 | IE No support No | Opera Full support 37 | Safari Full support 14 | WebView Android Full support 50 | Chrome Android Full support 50 | Firefox Android No support No | Opera Android Full support 37 | Safari iOS Full support 14 | Samsung Internet Android Full support 5.0 | nodejs No support No |
Legend
- Full support
- Full support
- No support
- No support
- User must explicitly enable this feature.
- User must explicitly enable this feature.