symbol.toString() returns the symbol as text. The text always has the form "Symbol(description)". For Symbol("userId") you get "Symbol(userId)", and for a symbol with no description you get "Symbol()".
You need this method because JavaScript does not turn a symbol into text on its own. Writing "Key: " + symbol or putting a symbol inside a template literal throws a TypeError. Calling toString() (or String(symbol)) is the safe way to print a symbol.
Remember
symbol.toString()
Symbol("id") → "Symbol(id)"
Symbol() → "Symbol()"
"a" + symbol → TypeError
`${symbol}` → TypeError
symbol.toString() → works
String(symbol) → works
symbol.description → just "id"
Syntax
Call it on a symbol. It takes no arguments and returns a string.
// ECMAScript 2015 (ES6)
Symbol.prototype.toString()
// returns: a string like "Symbol(description)"
// throws : TypeError if called on something that is not a symbol
Parameters
Parameter
Description
(none)
This method takes no parameters. Call it as symbol.toString().
Return Value
Return Value
Description
string
The text "Symbol(description)". If the symbol has no description, the text is "Symbol()".
TypeError (thrown)
Only when the method is borrowed and called on a value that is not a symbol, for example Symbol.prototype.toString.call("text").
JavaScript Examples
Example 1 — Turn Symbols into Text
Print a named symbol, an unnamed one, a shared one, and a built-in one.
Named symbol. The description "userId" is put between the brackets: Symbol(userId).
No description.Symbol() has nothing to show, so the brackets stay empty.
Shared and built-in symbols. A shared symbol shows its key as the description. The built-in Symbol.iterator has the description "Symbol.iterator", so you see Symbol(Symbol.iterator).
Example 2 — See Why + and Template Literals Fail
Join a symbol to text with + and with a template literal, and then use the two safe ways. The catch block prints the error type only.
Plus operator: TypeError
Template literal: TypeError
toString(): Symbol(userId)
String(): Symbol(userId)
How It Works
The + operator refuses. To join a symbol with text, JavaScript would have to turn the symbol into a string by itself. It is not allowed, so a TypeError is thrown before anything is printed.
Template literals refuse too.${id} needs the same automatic conversion and throws a TypeError.
Ask explicitly.id.toString() and String(id) both turn the symbol into text on purpose, so they work and print Symbol(userId).
Example 3 — List the Symbol Keys of an Object
Give an object two symbol keys, and show them as text. Normal tools such as Object.keys() do not list them.
Symbol keys: Symbol(id), Symbol(secret)
Object.keys: name
Type check: [object Symbol]
How It Works
Two hidden keys.ID and SECRET are used as property names, next to the normal property name.
Find and print them.Object.getOwnPropertySymbols(user) returns the symbol keys in the order they were added. map() calls toString() on each one so they can be joined into one line.
Normal tools skip them.Object.keys() shows only name. The last line shows that Object.prototype.toString reports the type [object Symbol].
Compatibility
Browser & Runtime Support
Symbol.prototype.toString() is part of ES2015 (ES6). Every current browser and Node.js has it.
✓ ES2015 · Widely supported
symbol.toString()
Supported in Chrome 38+, Edge 12+, Firefox 36+, Safari 9+, Opera 25+ and Node.js 0.12+. Internet Explorer never got it.
ES2015Standard since
Google Chrome38+ · Desktop & Mobile
Full support
Microsoft Edge12+ · Chromium and legacy
Full support
Mozilla Firefox36+ · Desktop & Mobile
Full support
Apple Safari9+ · macOS & iOS
Full support
Opera25+ · Chromium based
Full support
Samsung InternetRecent versions · Android
Supported
Android WebView38+ · Chromium based
Supported
Node.js0.12+ · Server runtime
Supported
DenoSupported · JavaScript runtime
Supported
BunSupported · JavaScript runtime
Supported
Internet ExplorerNo native support
Not supported
symbol.toString()Excellent
Bottom line: Safe to use in every current browser and in Node.js. The only thing to plan for is Internet Explorer, which has no Symbol at all.
Frequently Asked Questions
Use symbol.toString() or String(symbol). Both return text like "Symbol(id)". If you only want the label, use symbol.description, which returns "id".
JavaScript does not convert symbols to text automatically. This stops you from turning a symbol into an ordinary string key by accident. Convert on purpose with toString(), String() or description.
toString() returns the full text "Symbol(id)". The description property returns only the label "id", and it is undefined when the symbol has no description. Use toString() for logs and description when you need the bare text.
No. Two different symbols with the same description give the same text, so Symbol("a").toString() === Symbol("a").toString() is true. The text is only for display. Compare the symbols themselves with ===.
Yes. console.log(symbol) prints the symbol as Symbol(id), because it handles symbols itself. The error only appears when your own code joins a symbol to text with + or a template literal.