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 −
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } div { font-size: 20px; font-weight: 500; } </style> </head> <body> <h1>JavaScript Symbol.toStringTag Symbol</h1> <div class="sample"></div> <button class="Btn">CLICK HERE</button> <h3> Click on the above button to get the string description for objects </h3> <script> 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>"; }); </script> </body> </html>
Output
On clicking the “CLICK HERE” button −