jskatas.org Continuously Learn JavaScript. Your Way.

Symbol: Symbol.keyFor()

Symbol.keyFor() gets the symbol key for a given symbol.

Symbol.keyFor() gets the symbol key for a given symbol

pass the symbol to keyFor() and you get its key
const key = Symbol.____(Symbol.for('foo')); assert.equal(key, 'foo');
local symbols are not in the runtime-wide registry
// Hint: `Symbol()` creates a local symbol! const localSymbol = Symbol.for('foo'); const key = Symbol.keyFor(localSymbol); assert.equal(key, void 0);
predefined symbols are not in the runtime-wide registry either
const key = Symbol.keyFor(Symbol.iteraTor); assert.equal(key, void 0);
for non-Symbols throws an error
function fn() { Symbol.keyFor(Symbol.for('foo')); } assert.throws(fn);

Required Knowledge

Related Katas

Symbol

Difficulty Level

INTERMEDIATE

First Published

7 May 2015

Stats

4 tests to solve