The Symbol.keyFor function in JavaScript utilizes the global symbol registry to retrieve the key associated with a given symbol. Consequently, it is ineffective for symbols that are not global. If the symbol in question is not part of the global registry, the method will fail to locate it and will consequently return undefined.
Syntax
Example
Symbol.keyFor(Symbol);
Parameters
Symbol(required) : The symbol to find a key.
Return value
A string that denotes the key for the specified symbol, provided that it exists within the global registry.
Browser Support
| Chrome | 40 |
|---|---|
| Safari | 9 |
| Firefox | 36 |
| Opera | Yes |
Example 1
Example
<script>
//JavaScript to illustrate Symbol.keyFor
var i = Symbol.for("Example");
var j = Symbol.for("Java");
document.write(Symbol.keyFor(i));
document.write("<br>");
document.write(Symbol.keyFor(j));
//expected output: Example
//Java
</script>
Output:
Output
Example
Java
Example 2
Example
<script>
//JavaScript to illustrate Symbol.keyFor
var pyVisualizer = Symbol.for('Done');
var Java = Symbol.for('Done');
document.write(pyVisualizer === Java);
document.write("<br>");
document.write(Symbol.keyFor(pyVisualizer));
//expected output: true
//Done
</script>
Output:
Output
true
Done