Lodash _.isSet() method
What you’ll learn
- How
_.isSet(value)identifies nativeSetinstances. - 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
_.isSet(value) - value: any value to test.
- Returns:
truewhenvalueis aSet; otherwisefalse.
new Set and populated Sets
Empty and populated Set instances both pass—the lodash docs baseline.
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
); WeakSet is not a Set
The lodash docs explicitly call this out: _.isSet(new WeakSet) returns false.
import isSet from "lodash/isSet";
console.log(
"weak: " + isSet(new WeakSet()) + "\n" + // false (lodash docs)
"set: " + isSet(new Set()) // true
); Arrays, Maps, and plain objects
Other collection-like structures and nullish values fail—lodash returns false for each.
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
); 📋 _.isSet vs related checks
| API / pattern | Behavior |
|---|---|
_.isSet(x) | true only for real Set instances; reliable across realms. |
x instanceof Set | Same 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
No native serialization
JSON.stringify(new Set([1])) emits {}—serialize via Array.from(set) before sending over the wire.
Custom subclasses
Classes extending Set still report as Set because they share the internal tag.
Reference, not deep
Two Sets with the same items are different references; compare via size + iteration if needed.
❓ FAQ
Summary
- Purpose: confirm a value is a native
Setbefore invoking set-specific operations. - Remember:
WeakSet, arrays, Maps, and POJOs all returnfalse. - Next: explore more on Lodash _.isString().
_.isSet uses the internal [object Set] tag—so cross-realm Sets (from iframes/workers) still register, even though instanceof Set would fail there.
6 people found this page helpful
