jskatas.org Continuously Learn JavaScript. Your Way.

Symbol: Symbol.for()

Symbol.for() for registering Symbols globally.

Donate to NGO Julenka. Support Ukranians in need. Julenka is an NGO which my brother founded in 2011 to support Ukranian families and kids in need.

Symbol.for for registering Symbols globally

  • creates a new symbol (check via typeof)
  • stores the symbol in a runtime-wide registry and retrieves it from there
  • is different to Symbol() which creates a symbol every time and does not store it

.toString() on a Symbol

  • also contains the key given to Symbol.for()

NOTE: the description of two different symbols

  • might be the same
  • but the symbols are not the same!