WeakMap.prototype.get() - JavaScript | MDN

Try it

const weakmap = new WeakMap();
const object1 = {};
const object2 = {};

weakmap.set(object1, 42);

console.log(weakmap.get(object1));
// Expected output: 42

console.log(weakmap.get(object2));
// Expected output: undefined

Syntax

Parameters

key

The key of the value to return from the WeakMap object. Object keys are compared by reference, not by value.

Return value

The value associated with the specified key in the WeakMap object. If the key can't be found, undefined is returned. Always returns undefined if key is not an object or a non-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® 2027 Language Specification
# sec-weakmap.prototype.get

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on by MDN contributors.