How it works...

When creating a Symbol with the Symbol.for method, the instance returned may be a preexisting instance. We can see this when we create a Symbol with the same string and number values. We can even see the same instance when a number is provided as a string.

When printing out the types for the keys, we can see why the numbers match, even when the argument is a string. When we retrieve a key for a numeric value, it is converted to a string, and thus is equivalent to the string representation of the number.

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.