WeakMap.prototype.get()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Theget() method ofWeakMap instances returns the value corresponding to the key in thisWeakMap, orundefined if there is none. Object values are returned as the same reference that was originally stored, not as a copy, so mutations to the returned object will be reflected anywhere that reference is held, including inside theWeakMap.
In this article
Try it
const weakmap = new WeakMap();const object1 = {};const object2 = {};weakmap.set(object1, 42);console.log(weakmap.get(object1));// Expected output: 42console.log(weakmap.get(object2));// Expected output: undefinedSyntax
js
get(key)Parameters
Return value
The value associated with the specified key in theWeakMap object. If the key can't be found,undefined is returned. Always returnsundefined ifkey is not an object or anon-registered symbol.
Examples
>Using get()
js
const wm = new WeakMap();wm.set(window, "foo");wm.get(window); // Returns "foo".wm.get("baz"); // Returns undefined.Specifications
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-weakmap.prototype.get> |