Lodash _.isWeakMap() method

Beginner
⏱️ 5 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
Lodash

What you’ll learn

  • How _.isWeakMap(value) identifies native WeakMap instances.
  • Why Map, WeakSet, and plain objects fail the check.
  • How tag-based detection stays correct across realms.
  • Where weak maps fit—private metadata, DOM-keyed caches, and memoization.

Prerequisites

You know the difference between Map (strong keys) and WeakMap (weak object keys).

  • You have used new WeakMap() for per-instance bookkeeping.
  • Try-it labs load lodash from the CDN.

Overview

Use _.isWeakMap before invoking weak-map-only operations or when wiring up a cache that must rely on garbage-collection-friendly semantics.

Native WeakMap

Matches new WeakMap(); rejects Map, Set, WeakSet.

Cross-realm safe

Internal tag check covers cases where instanceof fails.

Memory-aware

Gate logic that depends on weak-reference garbage collection.

Syntax

javascript
_.isWeakMap(value)
  • value: any value to test.
  • Returns: true when value is a WeakMap; otherwise false.
1

new WeakMap instances

The lodash docs baseline: empty or populated WeakMap instances all qualify.

javascript
import isWeakMap from "lodash/isWeakMap";

const key = {};
const empty = new WeakMap();
const seeded = new WeakMap([[key, "value"]]);

console.log(
  "empty: " + isWeakMap(empty) + "\n" +     // true (lodash docs)
  "seeded: " + isWeakMap(seeded)             // true
);
Try it Yourself
2

Map is not a WeakMap

Lodash docs explicitly contrast: _.isWeakMap(new Map) returns false.

javascript
import isWeakMap from "lodash/isWeakMap";

console.log(
  "map: " + isWeakMap(new Map()) + "\n" +    // false (lodash docs)
  "weakSet: " + isWeakMap(new WeakSet())     // false
);
Try it Yourself
3

Plain objects, arrays, and nullish

Objects that mimic the WeakMap API still fail—lodash checks the internal tag, not duck-typed shape.

javascript
import isWeakMap from "lodash/isWeakMap";

const fake = { get() {}, set() {} };

console.log(
  "fake: " + isWeakMap(fake) + "\n" +       // false
  "arr: " + isWeakMap([]) + "\n" +           // false
  "nullVal: " + isWeakMap(null)               // false
);
Try it Yourself

📋 _.isWeakMap vs related checks

API / patternBehavior
_.isWeakMap(x)true only for native WeakMap; reliable across realms.
x instanceof WeakMapSame single-realm result; breaks across iframes/realms.
_.isMap(x)Companion check for strong-keyed Map instances.
_.isWeakSet(x)Detects WeakSet—different tag.

Pitfalls to avoid

Keys

Primitives forbidden

Adding a string or number key to a WeakMap throws—use object keys (or registered symbols in modern engines).

Iteration

No iteration / no size

By design, WeakMaps expose get, set, has, delete—but no iterator or count. Don’t reach for them when you need ordered or enumerable data.

JSON

Not serializable

JSON.stringify(new WeakMap()) drops the entries—persist any state to a real Map or array first.

❓ FAQ

WeakMap holds its keys weakly: when the only references to a key are inside the WeakMap, the entry can be garbage-collected. WeakMap also rejects primitive keys and is not iterable.
Map and WeakMap have distinct internal tags ([object Map] vs [object WeakMap]). Lodash checks for the WeakMap tag specifically.
No—there is no .keys(), .values(), .entries(), or size. That's intentional, to preserve the weak-reference semantics.
Yes. WeakMaps shine for per-instance metadata that should disappear when the host object goes away (private fields, memoization keyed by DOM nodes, etc.).

Summary

  • Purpose: verify a value is a native WeakMap before garbage-collection-aware bookkeeping.
  • Remember: Map, WeakSet, arrays, and POJOs always return false.
  • Next: explore more on Lodash _.isWeakSet().
Did you know?

WeakMap keys must be objects (or registered symbols in modern engines); primitive keys throw. Lodash’s _.isWeakMap guard is a quick way to confirm you actually have one before relying on those semantics.

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.

6 people found this page helpful