JavaScript | symbol.toString()

The symbol.toString() is an inbuilt function in JavaScript which is used to convert the specified symbol object into the string.

Syntax:

Symbol().toString();

Here Symbol() is the specified symbol object which is to be converted into string.

Parameters: This function does not accept any parameter.

Return value: This function returns the converted string of specified symbol object.

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
    // Creating some Symbol objects
    const A = Symbol('gfg');
    const B = Symbol(42);
    const C = Symbol();
  
    // Calling toString() function
    a = A.toString();
    b = B.toString();
    c = C.toString();
  
    // Printing the strings which represent
    // the above specified Symbol object
    document.write(a + "<br>");
    document.write(b + "<br>");
    document.write(c);
  
</script>

chevron_right


Output:

Symbol(gfg)
Symbol(42)
Symbol()

Example-2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
   
   // Creating some Symbol object and
   // Calling toString() function
   a = Symbol('Geeks').toString();
   b = Symbol.iterator.toString();
   c = Symbol.for('Geeks').toString();
      
   // Printing the strings which represent
   // the above specified Symbol object
   document.write(a +"<br>");
   document.write(b +"<br>");
   document.write(c);
       
</script>

chevron_right


Output:

Symbol(Geeks)
Symbol(Symbol.iterator)
Symbol(Geeks)

Supported Browsers:

  • Apple Safari 9
  • Edge 12
  • Firefox 36
  • Google Chrome 38
  • Opera 25

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



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.