Retrieving a symbol name in JavaScript

Having a variable with a symbol, you can retrieve its name in the global registry. This is done using the Symbol.keyFor method. Let's try. Let's create a symbol with a name:

let sym = Symbol.for('test');

Get its name by its variable:

let key = Symbol.keyFor(sym); console.log(key); // shows 'test'

Given two symbols:

let sym1 = Symbol.for('test1'); let sym2 = Symbol.for('test2');

Retrieve their names.