Documentación offline JavaScript main

WeakMap.prototype.get()

main Documentación oficial Licencia CC-BY-SA-2.5Descargado el 2026-08-02

En esta página

The get() method of {{jsxref("WeakMap")}} instances returns the value corresponding to the key in this WeakMap, or undefined 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 the WeakMap.

{{InteractiveExample("JavaScript Demo: WeakMap.prototype.get()")}}

```js interactive-example 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

```js-nolint
get(key)

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, {{jsxref("undefined")}} is returned. Always returns undefined if key is not an object or a non-registered symbol.

Examples#

Using get()#

const wm = new WeakMap();
wm.set(window, "foo");

wm.get(window); // Returns "foo".
wm.get("baz"); // Returns undefined.

Specifications#

{{Specifications}}

Browser compatibility#

{{Compat}}

See also#

  • {{jsxref("WeakMap")}}
  • {{jsxref("WeakMap.prototype.delete()")}}
  • {{jsxref("WeakMap.prototype.set()")}}
  • {{jsxref("WeakMap.prototype.has()")}}