WeakMap get() JavaScript

The JavaScript WeakMap get() method is used to retrieve the value of a specified key.

Syntax:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
WeakMapObj.get(key)
WeakMapObj.get(key)
WeakMapObj.get(key)

Parameters:
key: It represents the key of the element to be retrieved from the WeakMap object.

Returns:
It returns the value of a specified key

Example 1:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html>
<body>
<script>
var hello = new WeakMap();
var obj = {};
hello.set(obj, 'HELLO');
document.writeln(hello.get(obj));
</script>
</body>
</html>
<!DOCTYPE html> <html> <body> <script> var hello = new WeakMap(); var obj = {}; hello.set(obj, 'HELLO'); document.writeln(hello.get(obj)); </script> </body> </html>
<!DOCTYPE html>
<html>
<body>
<script>  
var hello = new WeakMap();  
var obj = {};  
hello.set(obj, 'HELLO');  
document.writeln(hello.get(obj));  
</script>
</body>
</html>

Example 2:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
const testWeakmap = new WeakMap();
const object1 = {};
const object2 = {};
testWeakmap.set(object1, 234);
document.write(testWeakmap.get(object1));
document.write("</br>");
document.write(testWeakmap.get(object2));
</script>
</body>
</html>
<!DOCTYPE html> <html> <body> <script type="text/javascript"> const testWeakmap = new WeakMap(); const object1 = {}; const object2 = {}; testWeakmap.set(object1, 234); document.write(testWeakmap.get(object1)); document.write("</br>"); document.write(testWeakmap.get(object2)); </script> </body> </html>
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript"> 
const testWeakmap = new WeakMap();
const object1 = {};
const object2 = {};
 
testWeakmap.set(object1, 234);
 
document.write(testWeakmap.get(object1));
document.write("</br>");
 
document.write(testWeakmap.get(object2));
</script>
</body>
</html>