JavaScript Symbol.keyFor() method

The JavaScript Symbol.keyFor() method is used to search for the key of a global symbol. It returns undefined if the symbol is not found.

Syntax:

Symbol.keyFor(symbol); 

Parameters:
symbol: It represents the specific symbol for which the key has to be fine.

Returns:
Key corresponding to a specific symbol.

Example 1:

<!DOCTYPE html>
<html>
<body>
<script>
var alpha = Symbol.for("a");
document.write(Symbol.keyFor(alpha));
</script>
</body>
</html>

Example 2:

<!DOCTYPE html>
<html>
<body>
<script>
const globalSymbol = Symbol.for('w3schools');
document.write(Symbol.keyFor(globalSymbol));
document.write("</br>");
const localSymbol = Symbol();
document.write(Symbol.keyFor(localSymbol));
document.write("</br>");
document.write(Symbol.keyFor(Symbol.iterator));
</script>
</body>
</html>