JavaScript | Symbol.toStringTag symbol

The Symbol.toStringTag is a well-known symbol and string valued property in JavaScript which is used in the creation of the default string description of an object.

Syntax:

Symbol.toStringTag

Parameters: This does not take any parameter.

Return value: This returns the String Object.

JavaScript code to show the working of this function.
Example-1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
    // Illustrating Symbol.toStringTag  
    console.log(Object.prototype.toString.call('Geeks'));     
    console.log(Object.prototype.toString.call("Geeks"));     
    console.log(Object.prototype.toString.call([1, 2, 3, 4]));    
    console.log(Object.prototype.toString.call(5));         
    console.log(Object.prototype.toString.call(true));    
    console.log(Object.prototype.toString.call(false));    
    console.log(Object.prototype.toString.call(undefined)); 
    console.log(Object.prototype.toString.call(null));  
</script>

chevron_right


Output:

> "[object String]"
> "[object String]"
> "[object Array]"
> "[object Number]"
> "[object Boolean]"
> "[object Boolean]"
> "[object Undefined]"
> "[object Null]"

Example-2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
    // Illustrating Symbol.toStringTag  
    class ToString {get [Symbol.toStringTag]() {  
       return 'GeeksforGeeks';  
      }  
    }
  
    // Geeting the string description of the object
    console.log(Object.prototype.toString.call(new ToString()));   
</script>

chevron_right


Output:

> "[object GeeksforGeeks]"

Supported Browsers:

  • Google Chrome 49
  • Firefox 51
  • Edge 15
  • Opera
  • Apple Safari

Reference: https://devdocs.io/javascript/global_objects/symbol/tostringtag



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.