Open In App

JavaScript Symbol toString() Method

Last Updated : 29 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The symbol.toString() is an inbuilt method in JavaScript that 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 a string. 

Parameters: This method does not accept any parameter. 

Return value: This method returns the converted string of the specified symbol object. JavaScript code to show the working of this method: 

Example-1: 

javascript




// Creating some Symbol objects
const A = Symbol('gfg');
const B = Symbol(42);
const C = Symbol();
 
// Calling toString() method
a = A.toString();
b = B.toString();
c = C.toString();
 
// Printing the strings which represent
// the above specified Symbol object
console.log(a);
console.log(b);
console.log(c);


Output:

Symbol(gfg)
Symbol(42)
Symbol()

Example-2: 

javascript




// Creating some Symbol object and
// Calling toString() method
a = Symbol('Geeks').toString();
b = Symbol.iterator.toString();
c = Symbol.for('Geeks').toString();
 
// Printing the strings which represent
// the above specified Symbol object
console.log(a);
console.log(b);
console.log(c);


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


Previous Article
Next Article

Similar Reads

JavaScript Symbol keyFor() Method
The Symbol.keyFor() is an inbuilt method in JavaScript that is used to retrieve the key which has been shared with the given symbols and this key is retrieved from the global symbol registry. Syntax: Symbol.keyFor(sym); Here "Symbol" is the symbol that is to be searched into the runtime-wide symbol registry. Parameters: This method accepts a parame
2 min read
JavaScript Symbol for() Method
The Symbol.for() is an inbuilt method in JavaScript that is used to search for the given symbol in a runtime-wide symbol registry and if found then it returns the same symbol otherwise it creates a new symbol with the same name of the given symbol into the global symbol registry and returns them. Syntax: Symbol.for(key); Here "Symbol" is the symbol
2 min read
JavaScript Symbol @@toPrimitive() Method
The symbol.@@toPrimitive() is an inbuilt method in JavaScript which is used to converts a given symbol object to a primitive value. Syntax: Symbol()[Symbol.toPrimitive](hint); Here Symbol() is the symbol object whose primitive value is to be found. Parameters: This method accepts an optional parameter "hint". Return value: This method returns the p
2 min read
JavaScript Symbol valueOf() Method
The symbol.valueOf() is an inbuilt method in JavaScript which is used to return the primitive value of a given symbol object. Syntax: Symbol().valueOf(); Here Symbol() is the symbol object whose primitive value is to be found. Parameters: This method does not take any parameter. Return value: This method returns the primitive value of the given sym
2 min read
JavaScript Symbol() Method
Symbols are new primitive built-in object types introduced as part of ES6. Symbols return unique identifiers that can be used to add unique property keys to an object that won’t collide with keys of any other code that might add to the object. They are used as object properties that cannot be recreated. It basically helps us to enable encapsulation
1 min read
JavaScript RegExp toString() Method
The RegExp toString() Method in JavaScript is used to return the string value of the regular expression. Syntax: RegExpObject.toString() Example 1: This example returns the string value of the regular expression. C/C++ Code function geek() { let regex = new RegExp("[a-c]", "gi"); let rex = regex.toString(); console.log(rex); } g
1 min read
JavaScript Number toString() Method
JavaScript Number toString() method in Javascript is used with a number and converts the number to a string. It is used to return a string representing the specified Number object. Syntax: num.toString(base) Parameters: This method accepts a single optional parameter base: It is an integer between 2 and 36 which is used to specify the base for repr
2 min read
JavaScript Array toString() Method
The JavaScript Array toString() Method returns the string representation of the array elements Syntax: arr.toString()Parameters: This method does not accept any parameter. Return value: The method returns the string representation of the array elements. If the array is empty, then it returns an empty string.The original array is not changed by this
2 min read
JavaScript Boolean toString() Method
The boolean.toString() method is used to return a string either "true" or "false" depending upon the value of the specified boolean object. Syntax: boolean.toString() Parameter: This method does not accept any parameter. Return Values: It returns a string either "true" or "false" depending upon the value of the specified boolean object. Below are e
3 min read
JavaScript String toString() Method
The JavaScript toString() method is an inbuilt function that returns the string itself. It does not modify the original string and it can be used to convert a string object to its string representation. Syntax: string.toString()Parameters: It does not accept any parameter. Return Values: It returns a new string representing the given string object.
2 min read