The JavaScript weakMap get method is used to retrieve the value of a specified key of an element from a WeakMap object.
Syntax
weakMap.get(key);
Parameters
key: The key of the element to return from the WeakMap object.
Return Value
It returns the value of a specified key. If the key can not be found, undefined is returned.
Example 1: How to Use JavaScript weakMap get() Method
let weakMap = new WeakMap();
let key1 = {};
let key2 = {};
weakMap.set(key1, "Taylor Swift");
weakMap.set(key2, "Selena Gomez");
console.log(weakMap.get(key1));
console.log(weakMap.get(key2));
Output
Taylor Swift
Selena Gomez
Example 2
let weakMap = new WeakMap();
let key1 = {};
let key2 = {};
weakMap.set(key1, 79);
console.log(weakMap.get(key1));
console.log(weakMap.get(key2));
Output
79
undefined
In the above example, it returns undefined because we haven’t set the value of key 2.
Browser Compatibility
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |
That’s it!
Related posts
JavaScript WeakMap set() Method
JavaScript WeakMap has() Method
JavaScript WeakMap delete() Method

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.