JavaScript Symbol.toStringTag symbol

JavascriptWeb DevelopmentObject Oriented Programming

The JavaScript Symbol.toStringTag symbol is a very well known symbol that is a string valued property used for giving a description of an object during its creation

Following is the code for Symbol.toStringTag symbol −


 Live Demo

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
   body {
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
   div {
      font-size: 20px;
      font-weight: 500;
<h1>JavaScript Symbol.toStringTag Symbol</h1>
<div class="sample"></div>
<button class="Btn">CLICK HERE</button>
Click on the above button to get the string description for objects
   let fillEle = document.querySelector(".sample");
   class testClass {
      get [Symbol.toStringTag]() {
         return 'Testing';
   class helloClass {
      get [Symbol.toStringTag]() {
         return 'Hello World';
   let test = new testClass();
   let hello = new helloClass();
   document.querySelector(".Btn").addEventListener("click", () => {
      fillEle.innerHTML += 'testClass = ' + test.toString() + "<br>";
      fillEle.innerHTML += 'helloClas = ' + hello.toString() + "<br>";


On clicking the “CLICK HERE” button −

Updated on 11-May-2020 09:30:17