1

As we’ve seen, usually all symbols are different, even if they have the same names. But sometimes we want same-named symbols to be same entities.

For instance, different parts of our application want to access symbol "id" meaning exactly the same property. Source.

Why not just export const mySymbol = Symbol("mySymbol"); instead? Then import it from anywhere in your application, making it pretty much a "global".

When is better to choose one over the other approach?

Community
  • 1
  • 1
ZenVentzi
  • 3,945
  • 3
  • 33
  • 48
  • If you `delete require.cache[filename]` and then require it again, it would be different object – AlexOwl Mar 12 '19 at 17:47
  • there is nothing in your linked article talking about exporting the symbol, these are unrelated concepts. your code is perfectly valid. – Steven Stark Mar 12 '19 at 17:48
  • 1
    Yes, if you can use modules and simply share a symbol by them, that's the better solution. The [use case of `Symbol.for`](https://stackoverflow.com/a/30985314/1048572) is when you cannot easily share a constant, but still need the same symbol. – Bergi Mar 12 '19 at 17:51

0 Answers0