Lodash _.isSet() method

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

What you’ll learn

  • How _.isSet(value) identifies native Set instances.
  • Why WeakSet, arrays, and Maps are excluded.
  • How the tag-based check stays reliable across realms.
  • Where this guard fits before set-only operations like add, has, or set union.

Prerequisites

You know the difference between Set, WeakSet, and ordinary arrays.

  • You have used new Set(iterable) for unique-value collections.
  • Try-it labs load lodash from the CDN.

Overview

Use _.isSet when an API or pipeline accepts “collection-like” inputs and you must branch on whether you got a real Set—not an array, Map, or weakset look-alike.

Native Set only

Matches new Set(); rejects WeakSet.

Cross-realm safe

Internal tag check works across frames where instanceof can fail.

Branch cleanly

Gate set-only operations before calling has, add, or spread.

Syntax

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

new Set and populated Sets

Empty and populated Set instances both pass—the lodash docs baseline.

javascript
import isSet from "lodash/isSet";

const empty = new Set();
const numbers = new Set([1, 2, 3]);

console.log(
  "empty: " + isSet(empty) + "\n" +     // true
  "numbers: " + isSet(numbers)          // true
);
Try it Yourself
2

WeakSet is not a Set

The lodash docs explicitly call this out: _.isSet(new WeakSet) returns false.

javascript
import isSet from "lodash/isSet";

console.log(
  "weak: " + isSet(new WeakSet()) + "\n" +    // false (lodash docs)
  "set: " + isSet(new Set())                    // true
);
Try it Yourself
3

Arrays, Maps, and plain objects

Other collection-like structures and nullish values fail—lodash returns false for each.

javascript
import isSet from "lodash/isSet";

console.log(
  "arr: " + isSet([1, 2, 3]) + "\n" +     // false
  "map: " + isSet(new Map()) + "\n" +      // false
  "obj: " + isSet({ size: 0 }) + "\n" +    // false
  "nullVal: " + isSet(null)                 // false
);
Try it Yourself

📋 _.isSet vs related checks

API / patternBehavior
_.isSet(x)true only for real Set instances; reliable across realms.
x instanceof SetSame single-realm result; breaks across iframes/realms.
_.isWeakSet(x)Complementary check for WeakSet instances.
_.isMap(x)Maps use a different internal tag—always false here.

Pitfalls to avoid

JSON

No native serialization

JSON.stringify(new Set([1])) emits {}—serialize via Array.from(set) before sending over the wire.

Subclasses

Custom subclasses

Classes extending Set still report as Set because they share the internal tag.

Equality

Reference, not deep

Two Sets with the same items are different references; compare via size + iteration if needed.

❓ FAQ

No. The lodash docs explicitly contrast new Set (true) with new WeakSet (false)—they are distinct built-ins with different tags.
Tag-based detection survives across realms (iframes, vm contexts) where instanceof can return false for legitimate Set objects from other realms.
No. Arrays with unique elements are still arrays. Construct an actual Set instance if you need lookup semantics.
Lodash only counts true Set instances; a plain object that mimics Set's API will return false.

Summary

  • Purpose: confirm a value is a native Set before invoking set-specific operations.
  • Remember: WeakSet, arrays, Maps, and POJOs all return false.
  • Next: explore more on Lodash _.isString().
Did you know?

_.isSet uses the internal [object Set] tag—so cross-realm Sets (from iframes/workers) still register, even though instanceof Set would fail there.

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