Basics of symbol primitive type
suggest changeSymbol is a primitive type commonly used for property keys. Each call to Symbol() creates a distinct value, even when two symbols have the same description, so these keys do not collide with string keys or independently created symbols. Symbol.for() is different: it retrieves or creates a symbol in a shared registry, so calls with the same registry key return the same symbol.
const MY_PROP_KEY = Symbol();
const obj = {};
obj[MY_PROP_KEY] = "ABC";
console.log(obj[MY_PROP_KEY]);
In this example, the result of console.log would be ABC.
You can also have named Symbols like:
const APPLE = Symbol('Apple');
const BANANA = Symbol('Banana');
const GRAPE = Symbol('Grape');
Each of these values is unique. The description does not make two Symbol() calls return the same value, and it is not a key you can use to look the symbol up.
The optional description is for debugging. To register or look up a shared symbol, see Symbol.for().
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents