Search code examples
javascriptsymbols

Get symbol name


How the symbol name can be get in form of string from value of type symbol? Symbol.prototype.toString() returns the name wrapped. E.g.

Symbol.iterator.toString()
// "Symbol(Symbol.iterator)"

Symbol('foo').toString()
// "Symbol(foo)"

Is there more direct way than parsing output of toString() method? For value Symbol('foo') I'd like to get just string foo.


Solution

  • Use the Symbol.prototype.description property:

    console.log(Symbol('foo').description)
    
    console.log(Symbol.iterator.description)