Learn JavaScript

Global Symbol

We can use Symbol.for() to create and access a global symbol.

In the example given above, the first Symbol.for() looks for the global symbol “Sam”. When it can not find the symbol, it creates the symbol. The second Symbol.for() again looks for the global symbol. Since the symbol is present, it can access the symbol.

Global symbols are accessible from anywhere in the code.