Search⌘ K
AI Features

Global Registry With Symbol

Explore the unique nature of JavaScript Symbols, how Symbol.for creates or retrieves Symbols from a global registry, and using Symbol.keyFor to find associated keys. Understand these concepts to manage special identifiers and improve your JavaScript code design.

Uniqueness of Symbols

When we create a Symbol using the Symbol() function, the argument passed to it has no significance, and each call to Symbol() creates a unique Symbol.

Proof by example

Let’s quickly verify this behavior with an example.

Javascript (babel-node)
'use strict';
//START:CODE
const name = 'Tom';
const tom = Symbol(name);
const jerry = Symbol('Jerry');
const anotherTom = Symbol(name);
console.log(tom);
console.log(typeof(tom));
console.log(tom === jerry);
console.log(tom === anotherTom);
//END:CODE

Explanation

  • We created three Symbols. Two of them were created using the same argument name.
  • However, since the arguments passed to the function have no significance and the Symbol
...