Symbol.prototype.description

Reads the debug label a symbol was created with, without the Symbol(...) wrapper text.

Signature
symbol.description
  • Added in JavaScript ES2019

Return value

string | undefinedThe description string given when the symbol was created, or undefined if none was supplied.

Errors

Throws a TypeError when the getter is accessed on a value that is not a symbol or Symbol wrapper object.

Changelog

  • PHP ES2019Added as a read-only accessor. Before it existed, code parsed the output of toString() to recover the label.

description is a read-only accessor on Symbol.prototype that returns the label passed to Symbol() or Symbol.for() — just the label, with no surrounding Symbol(…) text. It was added in ES2019 to replace the old trick of slicing toString().

When to reach for it

Use it whenever you are building a message for a human: log output, an error string naming which protocol key was missing, or a debug view of Object.getOwnPropertySymbols(). It is safe inside template literals because it yields a string (or undefined), whereas interpolating the symbol itself throws.

What trips people up

Three values look the same in a log but are not: Symbol().description is undefined, Symbol("").description is the empty string, and Symbol(undefined).description is undefined. If you are branching on the label, test with === undefined rather than truthiness. Remember too that the description carries no identity — two symbols with the same description are still different values — so never use it as an equality test or to reconstruct a symbol. Well-known symbols report their spec names, for example Symbol.iterator.description is "Symbol.iterator".

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Build readable log lines listing an object's symbol keys
  • Name the missing protocol symbol in a thrown error message
  • Show a friendly label in a debug inspector without triggering symbol coercion errors

Frequently asked questions

How do I get a symbol's name as a plain string?

Read its description property, which returns just the label with no Symbol(...) wrapper. Use toString() instead if you want the wrapped form "Symbol(label)".

Can I change a symbol's description?

No. description is a read-only getter on Symbol.prototype with no setter, and the label is fixed when the symbol is created.

What is the difference between description and toString()?

description returns "id" while toString() returns "Symbol(id)". description can also be undefined for a symbol created with no argument, whereas toString() then returns "Symbol()".

More examples

Listing an object's symbol keys readably

description gives plain strings, so the array prints cleanly and interpolates without error — interpolating the symbols themselves would throw a TypeError.

Example · javascript
const CACHE = Symbol("cache");
const SEEN = Symbol("seen");
const obj = { name: "node", [CACHE]: 1, [SEEN]: true };

const labels = Object.getOwnPropertySymbols(obj).map(
  (s) => s.description ?? "(anonymous)",
);
console.log(labels);
console.log(`first key: ${labels[0]}`);

Description is not identity

Both symbols share the description "token" yet remain distinct keys, so the object ends up with two separate symbol-keyed properties holding different values.

Example · javascript
const a = Symbol("token");
const b = Symbol("token");

console.log(a.description === b.description);
console.log(a === b);

const o = { [a]: 1, [b]: 2 };
console.log(Object.getOwnPropertySymbols(o).length);
console.log(o[a], o[b]);

Discussion

  • Be the first to comment on this lesson.