Using Symbol to create a local instance

Symbols on their own aren't particularly useful, but they are very useful as keys for other data structures. They are well suited as keys because it is possible to restrict the access to their values. There are two ways these comparisons can work. We can create local symbols, which are unique and can be recreated after initialization, and global symbols, which can be referenced by their constructor value.

In this recipe, we'll take a look at how to use Symbol as a function to create local symbols. This means that each instance will be new, even if the same arguments are used.

Get ECMAScript Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.