Lodash _.isMap() method
What you’ll learn
- How
_.isMap(value)distinguishes realMapinstances from look-alikes. - Why
WeakMap, POJOs, and other collections fail even when they feel similar. - How lodash picks Node’s native probe versus tag-based detection in browsers.
- When
instanceof Mapalone might mislead across realms—and lodash helps.
Prerequisites
You have created a new Map() before and know it differs from plain objects.
- You understand ES2015 collections (
Map,Set) at a basic level. - Try-it labs load lodash from the CDN (modern browsers).
Overview
Use _.isMap inside serializers, middleware normalizers, or deep-clone utilities whenever you branch on keyed insertion order semantics guaranteed by Map—not ordinary objects.
Real Maps
Accepts empty or populated Map instances from modern engines.
Engine-aware
Node prefers util.types.isMap; browsers fall back to tag detection.
WeakMap out
Non-enumerable weak-key semantics remain excluded—by design.
Syntax
_.isMap(value) - value: any value to test.
- Returns:
trueifvalueis classified as aMap; otherwisefalse.
Empty and populated Map instances
Fresh maps and maps built from iterables both identify as Maps.
import isMap from "lodash/isMap";
console.log(
"emptyMap: " + isMap(new Map()) + "\n" + // true
"withPairs: " + isMap(new Map([["key", 42]])) // true
); WeakMap, plain objects, and arrays
Different builtins and POJOs do not share the Map brand.
import isMap from "lodash/isMap";
console.log(
"weakMap: " + isMap(new WeakMap()) + "\n" + // false
"plainObj: " + isMap({ a: 1 }) + "\n" + // false
"array: " + isMap([["x", 1]]) // false (array of pairs, not Map)
); Map subclasses versus Set
Extending Map preserves branding; Set is still a different collection.
import isMap from "lodash/isMap";
class TaggedMap extends Map {}
console.log(
"subclass: " + isMap(new TaggedMap()) + "\n" + // true
"set: " + isMap(new Set()) // false
); 📋 _.isMap vs related checks
| API | Behavior |
|---|---|
_.isMap(x) | True for Map instances (tag/native probe); false for WeakMap. |
x instanceof Map | Usually aligned in one realm; can fail across iframes—lodash detection is more portable. |
_.isPlainObject(x) | Detects literal object prototypes—orthogonal to Map branding. |
_.isSet(x) | Companion helper for unordered unique-value collections. |
Pitfalls to avoid
Serialization round-trips
JSON.stringify does not preserve Maps by default—revived data usually becomes plain objects.
Library-specific map types
Immutable.js or other wrappers may not brand as native Map—verify their own predicates.
Legacy environments
Ensure a faithful Map implementation exists; incomplete polyfills can confuse tagging.
❓ FAQ
Summary
- Purpose: identify genuine
Mapinstances for branching logic. - Remember: duck-typed objects are never Maps without engine branding.
- Next: explore more on Lodash _.isMatch().
_.isMap delegates to util.types.isMap in Node when present; in browsers it falls back to isObjectLike plus Object.prototype.toString matching [object Map]—which excludes WeakMap ([object WeakMap]) and plain objects.
6 people found this page helpful
