Lodash _.isWeakSet() method

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

What you’ll learn

  • How _.isWeakSet(value) detects native WeakSet instances.
  • Why Set, WeakMap, and plain objects always return false.
  • How the internal-tag check survives cross-realm boundaries.
  • Where WeakSets fit—tagging objects without blocking garbage collection.

Prerequisites

You understand the difference between Set (iterable, strong refs) and WeakSet (object-only, weak refs).

  • You have used new WeakSet() to track objects (visited nodes, processed instances, etc.).
  • Try-it labs load lodash from the CDN.

Overview

Use _.isWeakSet before reaching for add/has/delete, or whenever you need to confirm a value really is a weak-reference set rather than a regular Set.

Native WeakSet

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

Cross-realm safe

Tag-based detection covers cases where instanceof would lie.

GC-friendly

Confirms a structure that won’t pin objects in memory.

Syntax

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

new WeakSet instances

The lodash docs baseline: a fresh WeakSet—empty or populated—always qualifies.

javascript
import isWeakSet from "lodash/isWeakSet";

const a = {};
const empty = new WeakSet();
const seeded = new WeakSet([a]);

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

Set is not a WeakSet

Lodash docs explicitly contrast: _.isWeakSet(new Set) returns false. WeakMaps fail too—different tag, different shape.

javascript
import isWeakSet from "lodash/isWeakSet";

console.log(
  "set: " + isWeakSet(new Set()) + "\n" +    // false (lodash docs)
  "weakMap: " + isWeakSet(new WeakMap())     // false
);
Try it Yourself
3

Arrays, plain objects, and nullish

Objects that mimic a set API still fail—lodash inspects the internal tag, not duck-typed methods.

javascript
import isWeakSet from "lodash/isWeakSet";

const fake = { add() {}, has() {} };

console.log(
  "fake: " + isWeakSet(fake) + "\n" +       // false
  "arr: " + isWeakSet([1, 2, 3]) + "\n" +    // false
  "nullVal: " + isWeakSet(null)               // false
);
Try it Yourself

📋 _.isWeakSet vs related checks

API / patternBehavior
_.isWeakSet(x)true only for native WeakSet; reliable across realms.
x instanceof WeakSetSame single-realm result; breaks across iframes/realms.
_.isSet(x)Companion check for iterable Set instances.
_.isWeakMap(x)Detects WeakMap—different tag.

Pitfalls to avoid

API

No .clear() or .size

WeakSet exposes only add, has, and delete. Calling weakSet.clear() throws “is not a function”—don’t copy that pattern from older snippets.

Values

Objects only

Adding a primitive (string, number) throws a TypeError. Wrap primitives in an object first or use a regular Set.

Iteration

Not iterable

There is no .values(), .forEach(), or for...of. If you need to enumerate, choose Set.

❓ FAQ

WeakSet only stores objects, holds them weakly (so they can be garbage-collected), and exposes none of Set's iteration helpers—no .size, .values(), or .clear().
Set and WeakSet have distinct internal tags ([object Set] vs [object WeakSet]). Lodash checks specifically for the WeakSet tag.
Yes—if you already know you have a WeakSet, just call .has(obj). Use _.isWeakSet only when the value's type is uncertain.
When you need to mark or tag objects (e.g., 'already seen', 'authorised') without preventing them from being garbage-collected.

Summary

  • Purpose: confirm a value is a native WeakSet before calling its tiny API surface.
  • Remember: Set, WeakMap, arrays, and POJOs always return false.
  • Next: browse the rest of Lodash _.lt().
Did you know?

WeakSet has only add, has, and delete—no clear(), no size, no iteration. Code that tries weakSet.clear() throws “is not a function”, which is why _.isWeakSet matters as a guard before you reach for set-like operations.

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.

5 people found this page helpful