JavaScript Symbols Using Symbol.for() to create global, shared symbols

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

The Symbol.for method allows you to register and look up global symbols by name. The first time it is called with a given key, it creates a new symbol and adds it to the registry.

let a = Symbol.for('A');

The next time you call Symbol.for('A'), the same symbol will be returned instead of a new one (in contrast to Symbol('A') which would create a new, unique symbol that happens to have the same description).

a === Symbol.for('A') // true

but

a === Symbol('A') // false


Got any JavaScript Question?