The Symbol.toStringTag symbol in JavaScript is utilized internally by the Object.prototype.toString function. This symbol plays a crucial role in generating the standard string representation of an object.
Syntax
Example
Symbol.toStringTag
Parameters
String
Return value
String Object.
Browser Support
| Chrome | 32 |
|---|---|
| Safari | 8 |
| Firefox | 29 |
| Opera | 19 |
Example 1
Example
<script>
//JavaScript to illustrate Symbol.toStringTag
class ToString {get [Symbol.toStringTag]() {
return 'Example';
}
}
document.write(Object.prototype.toString.call(new ToString()));
//expected output: [object Example]
</script>
Output:
Output
[object Example]
Example 2
Example
<script>
//JavaScript to illustrate Symbol.toStringTag
document.write(Object.prototype.toString.call(([1,2,3])));
document.write("<br>");
document.write(Object.prototype.toString.call((true)));
//expected output: [object Array]
//[object Boolean]
</script>
Output:
Output
[object Array]
[object Boolean]