JavaScript | weakMap.get()
The weakMap.get() is an inbuilt function in JavaScript which is used to return a particular element from a 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.
Code #1:
<script> // 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 document.write(weakmap1.get(key1)); </script> |
Output:
42
Code #2:
<script> // Creating a WeakMap() object const weakmap1 = new WeakMap(); // Creating a key "key1" const key1 = {}; // Getting the specified elements document.write(weakmap1.get(key1)); </script> |
Output:
undefined
Here output is undefined because the key “key1” has not been set at the end of the weakMap object.
Recommended Posts:
- Introduction to JavaScript Course | Learn how to Build a task tracker using JavaScript
- How to compare two JavaScript array objects using jQuery/JavaScript ?
- JavaScript Course | Understanding Code Structure in JavaScript
- JavaScript Course | Data Types in JavaScript
- JavaScript Course | Printing Hello World in JavaScript
- JavaScript Course | Logical Operators in JavaScript
- JavaScript Course | Conditional Operator in JavaScript
- JavaScript Course | Loops in JavaScript
- JavaScript Course | JavaScript Prompt Example
- JavaScript Course | Variables in JavaScript
- JavaScript Course | Functions in JavaScript
- JavaScript Course | Operators in JavaScript
- JavaScript Course | Objects in JavaScript
- JavaScript vs Python : Can Python Overtop JavaScript by 2020?
- How to include a JavaScript file in another JavaScript file ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.