Underscore.js _.isWeakMap() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Object

Definition

Returns true if object is a WeakMap (official Underscore docs). new WeakMap() passes; plain objects, Map, and other collections do not. Stable helper (Underscore 1.9+) — not deprecated.

Pass the required arguments shown below. Underscore object helpers usually return a new value — check whether the source object is mutated.

Syntax

Call _.isWeakMap. Start with this line.

Underscore
_.isWeakMap(object)
CallWhat it does
_.isWeakMap(object)Primary signature.
_.isWeakMap(...)Call with the arguments your case needs.

Load Underscore with import _ from "underscore" (or the UMD CDN build) so these helpers are available as _.

Library declarations
Underscore
_.isWeakMap(object)

Parameters

ParameterDescription
objectRequired. Value to test

Return Value

Return ValueDescription
*true or false

Underscore.js Examples

Example 1 — Official: new WeakMap()

The docs check an empty WeakMap instance.

Underscore
_.isWeakMap(new WeakMap());
// → true
Try It Yourself

How It Works

  1. Create. new WeakMap() builds an empty WeakMap.
  2. Tag check. Underscore sees [object WeakMap] → true.
  3. Official example. This is the sample from the Underscore documentation.
  4. Read-only. Nothing is mutated; you only get a boolean.

Example 2 — WeakMap With Entries

Having keys stored does not change the type check.

Underscore
var meta = new WeakMap();
var user = { name: "moe" };
meta.set(user, { role: "admin" });

_.isWeakMap(meta);
// → true

meta.get(user);
// → { role: "admin" }
Try It Yourself

How It Works

  1. Object key. WeakMap keys must be objects — here user.
  2. Store metadata. .set() attaches private data without adding properties to user.
  3. Still a WeakMap. Entries do not change the type → isWeakMap stays true.
  4. Read back. .get(user) returns the stored value.

Example 3 — Map and Plain Object Fail

Related collections are not WeakMaps — use the matching helper.

Underscore
_.isWeakMap(new Map());  // → false
_.isWeakMap({});          // → false
_.isWeakMap([]);          // → false

_.isMap(new Map());      // → true
_.isObject({});          // → true
Try It Yourself

How It Works

  1. Map. Different tag — use _.isMap.
  2. Plain object. Looks like a bag of keys, but is not a WeakMap.
  3. Array. Not a WeakMap either.
  4. Pick the helper. Match the check to the collection you actually have.

Example 4 — Guard Before .get()

Only call WeakMap methods after confirming the type.

Underscore
function readMeta(store, key) {
  if (!_.isWeakMap(store)) {
    return null;
  }
  return store.has(key) ? store.get(key) : null;
}

var wm = new WeakMap();
var obj = {};
wm.set(obj, 42);

readMeta(wm, obj);       // → 42
readMeta(new Map(), obj); // → null
readMeta({}, obj);        // → null
Try It Yourself

How It Works

  1. Guard first. Skip non-WeakMaps so .get() never runs on the wrong type.
  2. WeakMap path. Stored value 42 is returned for obj.
  3. Fail closed. Map and plain objects return null.
  4. Pattern. Type-check → then call collection methods.

Browser & Runtime Support

_.isWeakMap ships with Underscore.js 1.9+. It needs an ES2015+ runtime that provides WeakMap. Works in modern browsers (CDN or bundle) and current Node.js / Bun / Deno with npm install underscore.

✓ Stable · Underscore 1.9+

_.isWeakMap()

Available in [email protected]+ (UMD / ESM). Requires native WeakMap (ES2015). Fine on evergreen browsers and current Node LTS when Underscore is loaded.

98% Modern ES2015+ runtimes
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
_.isWeakMap Underscore 1.9+ · ES2015+

Bottom line: Safe for modern apps once Underscore 1.9+ is loaded. Not a native browser API by itself — you need both Underscore and a WeakMap-capable engine. Prefer a pinned CDN version (e.g. [email protected]).

Frequently Asked Questions

_.isWeakMap(object) returns true only when the value is a WeakMap — for example new WeakMap(). Plain objects, Map, Set, WeakSet, arrays, null, and undefined return false.
_.isWeakMap(new WeakMap()) returns true. The docs pick a real WeakMap instance on purpose — that is the type the helper looks for.
No. Map and WeakMap are different types. WeakMap keys must be objects and are held weakly so they do not prevent garbage collection.
typeof new WeakMap() is "object", so typeof is too broad. Underscore uses the [object WeakMap] tag (or an IE11 fingerprint), which is more precise.
Use it as a guard before calling .get(), .set(), .has(), or .delete() when a value might be a WeakMap, a Map, or a plain object.
No. It is a stable Underscore helper added in version 1.9.0. Load Underscore 1.9+ in an ES2015+ environment and call _.isWeakMap(value).

Next: _.isArrayBuffer()

Learn how to detect ArrayBuffer — the raw byte storage behind typed views.

_.isArrayBuffer() 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.

10 people found this page helpful