The Javascript weakMap.get() is an inbuilt function in JavaScript that is used to return a particular element from an object WeakMap.
Syntax:
weakMap.get(key);
Parameters: It accepts a parameter “key” which is the key of the element which is going to be returned from the object weakmap.
Return values: It returns the element which is associated with the particular key in the WeakMap object and it returns undefined if the key can not be found.
Below is an example of weakMap.get() method.
Example 1: This example shows the basic use of the weakmap().get method.
javascript
function gfg() { const weakmap1 = new WeakMap(); const key1 = {}; weakmap1.set(key1, 12); console.log(weakmap1.get(key1)); } gfg(); |
12
Example 2: Here we get the output as 42 because we have set the value to 42.
javascript
// Creating a WeakMap() object const weakmap1 = new WeakMap(); // Creating a key "key1" const key1 = {}; // setting value 42 with key "key1" in the // object weakMap weakmap1.set(key1, 42); // Getting the specified elements i.e, 42 console.log(weakmap1.get(key1)); |
42
Example 3: In this example, we will see the basic use of weakmap.get() method,.Here the output is undefined because the key “key1” has not been set at the end of the weakMap object.
javascript
// Creating a WeakMap() object const weakmap1 = new WeakMap(); // Creating a key "key1" const key1 = {}; // Getting the specified elements console.log(weakmap1.get(key1)); |
undefined
We have a complete list of Javascript WeakMap methods, to check those please go through Javascript WeakMap Complete reference article.
Supported Browsers:
- Google Chrome 36 and above
- Edge 12 and above
- Firefox 6 and above
- Internet Explorer 11 and above
- Opera 23 and above
- Safari 8 and above