ES6 - Symbol.keyFor


Advertisements

This method retrieves a shared symbol key from the global symbol registry for the given symbol.

Syntax

The syntax for Symbol.keyFor is mentioned below where, sym is the symbol to find a key for.

Symbol.keyFor(sym)

Example

<script>
   const user_Id = Symbol.for('userId') // creates a new Symbol in registry
   console.log(Symbol.keyFor(user_Id)) // returns the key of a symbol in registry
   const userId = Symbol("userId")// symbol not in registry
   console.log(Symbol.keyFor(userId)) //userId symbol is not in registry
</script>

The output of the code is as given below −

userId
undefined
Advertisements