The JavaScript Symbol.for() method is used to find out the existing symbol in a runtime-wide symbol registry with the provided key. It will create a new symbol if it does not exist for the specified key.
Syntax:
Symbol.for(key);
Parameters:
key: It represents the specific key for which the symbol has to be fine.
Returns:
Symbol corresponding to a specific key.
Example 1:
<!DOCTYPE html> <html> <body> <script> var alpha = Symbol.for("!"); var beta = Symbol.for("!"); document.write(alpha == beta); </script> </body> </html>
Example 2:
<!DOCTYPE html> <html> <body> <script> document.write(Symbol.for('w3schools') === Symbol.for('w3schools')); document.write("</br>"); document.write(Symbol('w3schools') === Symbol('w3schools')); document.write("</br>"); const symbol = Symbol.for('Jai'); document.write(symbol.toString()); </script> </body> </html>