C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
JavaScript Symbol.for() MethodThe JavaScript Symbol.for() method is used to search for existing symbol in a runtime-wide symbol registry with the provided key and returns if it found. Otherwise, a new symbol gets created with this key. Syntax
Symbol.for(key); Parameters
Key: The Keys to the symbol. Return valueExisting symbol with the given key if found otherwise, a new symbol is created and returned. Browser Support
Example 1
<script>
//JavaScript to illustrate Symbol.for
// read from the registry
// if the symbol did not exist, it is created
var i = Symbol.for("i");
var o = Symbol.for("i");
document.write(i==o);
//expected output: true
</script>
Output: true Example 2
<script>
//JavaScript to illustrate Symbol.key
// read from the registry
var JavaTpoint = Symbol.for('hello'); // If the Symbol does not exist, it's created
var Java = Symbol.for('hello'); // The Symbol exists, so it is returned
document.write(JavaTpoint === Java); // true
//expected output: true
</script>
Output: true
Next TopicJavaScript Symbol
|