The JavaScript WeakMap get() method is used to retrieve the value of a specified key.
Syntax:
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:
<!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:
<!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>