Symbol, which dynamically produces an anonymous, unique value. A symbol may be used as an object property.
Symbol can have an optional description, but for debugging purposes only.
A Symbol value represents a unique identifier. For example:
// Here are two symbols with the same description: let Sym1 = Symbol("Sym") let Sym2 = Symbol("Sym") console.log(Sym1 === Sym2) // returns "false" // Symbols are guaranteed to be unique. // Even if we create many symbols with the same description, // they are different values.
Symbol type is a new feature in ECMAScript 2015. There is no ECMAScript 5 equivalent for Symbol.
In some programming languages, the symbol data type is referred to as an "atom."
Symbols don't "Auto-Convert" to strings
alert almost any value, and it will work. Symbols are special. They don’t auto-convert.
let Sym = Symbol("Sym") alert(Sym) // TypeError: Cannot convert a Symbol value to a string
That’s a "language guard" against messing up, because strings and symbols are fundamentally different, and should not occasionally convert one into another.
If you really want to show a symbol, we need to call
.toString() on it.
let Sym = Symbol("Sym") alert(Sym.toString()) // Symbol(Sym), now it works
Or you can use the
symbol.description property to get its description:
let _Sym = Symbol("Sym"); alert(_Sym.description); // Sym
Symbol class has constants for so-called well-known symbols. These symbols let you configure how JS treats an object, by using them as property keys.
They are listed in the specification in the Well-known symbols table:
- …and so on.
Global symbol registry
There is a global symbol registry holding all available symbols. The methods that access the registry are
Symbol.for(tokenString) returns a symbol value from the registry, and
Symbol.keyFor(symbolValue) returns a token string from the registry; each is the other's inverse, so the following is
Symbol.keyFor(Symbol.for("tokenString")) === "tokenString" // true