CodesJava

Easy learning with example program codes

JavaScript Symbol.keyFor() method

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

Syntax:

Symbol.keyFor(symbol); 

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

Returns:
Key corresponding to specific symbol.

Example 1:

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

Output:

a

Example 2:

<!DOCTYPE html>
<html>
<body>
<script>   
const globalSymbol = Symbol.for('codesjava');
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>

Output:

codesjava
undefined
undefined
Please follow and like us:
error








Copyright © 2019 CodesJava DMCA.com Protection Status