JavaScript WeakMap get() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
WeakMap

Definition

weakMap.get(key) returns the value that was stored for key, or undefined when there is no such key. It never throws: a string, a number, or null simply gives undefined, because those can never be keys. Keys are matched by identity, so a different object with the same shape is a different key. Use has() when you need to tell a missing key from a stored undefined.

Remember
weakMap.get(key)  → the stored value, or undefined

key is stored       → its value
key not stored      → undefined
key is not object   → undefined (no error)
different object    → undefined (same shape is not enough)

stored undefined looks like missing: ask has()

Syntax

Call get on a WeakMap with the key you used in set.

JavaScript
weakMap.get(key)
CallWhat it does
weakMap.get(key)Return the value stored for key, or undefined.
weakMap.get(key) ?? fallbackUse ?? to supply a default when nothing is stored (or when the stored value is null).

Built into JavaScript since ES2015. get() only reads: it never adds a key. To read and also store a default, see getOrInsert().

Library declarations
JavaScript
weakMap.get(key)   // returns the value, or undefined

// key: any value; only objects (and local symbols) can ever match
// never throws

Parameters

ParameterDescription
keyRequired. The same object (or local symbol) that was passed to set(). Any other value, including primitives, just gives undefined.

Return Value

Return ValueDescription
any valueThe value stored for the key.
undefinedNo entry for this key, or the stored value is undefined.

JavaScript Examples

Example 1 — Read a Stored Value

Read a note that was stored for an object, and see what other lookups return.

JavaScript
const notes = new WeakMap();
const page = { id: "home" };
const other = { id: "about" };

notes.set(page, "Needs a new banner");

console.log(notes.get(page));                  // Needs a new banner
console.log(String(notes.get(other)));         // undefined
console.log(String(notes.get({ id: "home" })));  // undefined
console.log(String(notes.get("home")));        // undefined
Try It Yourself

How It Works

  1. A stored key gives its value. notes.get(page) uses the very same object that was passed to set(), so it finds the note.
  2. Other objects give nothing. other was never stored, so get() answers undefined. It does not add a new entry.
  3. Matching is by identity. A new { id: "home" } looks the same but is a different object. A plain string is not a key at all, and neither case throws.

Example 2 — Stored undefined or Missing Key?

Both look the same to get(). Ask has() to tell them apart.

JavaScript
const wm = new WeakMap();
const stored = {};
const missing = {};

wm.set(stored, undefined);

console.log(String(wm.get(stored)));   // undefined
console.log(String(wm.get(missing)));  // undefined
console.log(wm.has(stored));           // true
console.log(wm.has(missing));          // false
Try It Yourself

How It Works

  1. Undefined can be a real value. wm.set(stored, undefined) is allowed. The entry exists even though its value is undefined.
  2. get() cannot tell the two apart. Both lookups print undefined, so the answer alone does not say whether the key is in the WeakMap.
  3. has() settles it. has(stored) is true and has(missing) is false. Ask it first when a stored undefined is possible.

Example 3 — Cache a Result for Each Object

Do the work once per object. The second call finds the saved answer with get().

JavaScript
const cache = new WeakMap();

function area(shape) {
  const saved = cache.get(shape);
  if (saved !== undefined) {
    return saved + " (cached)";
  }
  const value = shape.w * shape.h;
  cache.set(shape, value);
  return String(value);
}

const box = { w: 3, h: 4 };
console.log(area(box));                // 12
console.log(area(box));                // 12 (cached)
console.log(area({ w: 3, h: 4 }));     // 12
Try It Yourself

How It Works

  1. Look first, work second. The function calls cache.get(shape) before doing any math. undefined means nothing is saved yet.
  2. Save for next time. On the first call it multiplies 3 by 4, stores 12 with set(), and returns it. The second call returns the saved answer.
  3. A new object starts fresh. The third call passes a different object, so get() finds nothing and the answer is computed again. Old shapes can be cleaned up automatically.

Browser & Runtime Support

WeakMap.prototype.get() is part of ES2015 (ES6), together with Map, Set, and WeakSet.

✓ Baseline · ES2015

WeakMap.prototype.get()

Works in every modern browser, plus Node.js, Deno, and Bun. Internet Explorer 11 has a basic WeakMap with set(), get(), has(), and delete(), but not the constructor argument.

98% Modern browser support
Google Chrome 36+ · Desktop & Mobile
Full support
Mozilla Firefox 6+ · Desktop & Mobile
Full support
Apple Safari 8+ · macOS & iOS
Full support
Microsoft Edge 12+
Full support
Node.js 0.12+ · every current release
Supported
Deno All releases
Supported
Internet Explorer IE 11 · no constructor argument
Basic
WeakMap.prototype.get() Excellent

Bottom line: Safe to use in any modern browser and runtime. get() has behaved the same way since WeakMap first shipped, and it never throws.

Frequently Asked Questions

It returns the value stored for the key, or undefined if the key is not in the WeakMap. It never changes the WeakMap and never adds a key.
No. Strings, numbers, and other plain values can never be keys, so get() just returns undefined. Only set() throws for such keys.
Call has(key). It returns true when the key is stored, even if the value is undefined, and false when it is missing. get() cannot tell the two apart.
WeakMap compares keys by identity, not by content. Two objects with the same fields are two separate keys. Keep a reference to the exact object you used in set().
It is similar but the data lives in the WeakMap, not on the object. That is the point: you can attach data to an object you do not own without changing it, and the data goes away when the object does.

Next: getOrInsert()

Read a value, or store a default first when the key is missing.

getOrInsert() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful