Map
The Map global object holds key-value pairs, accepts any value as a key, including objects and functions, and remembers the order in which keys were first added. Keys keep their original type instead of becoming strings the way non-symbol property names on a plain Object do, so an object can serve as a key:
> const ada = { name: "Ada" };
undefined
> const lastSeen = new Map([[ada, "2026-11-09"]]);
undefined
> lastSeen.get(ada)
'2026-11-09'
> lastSeen.size
1
Keys are matched with the SameValueZero algorithm. It works like ===, except that NaN matches NaN. Objects match only themselves, so two objects with the same contents are two different keys.
Click a key below to store it in a plain object and in a Map with the same value, and compare which keys end up as separate entries:
Syntax
The Map constructor takes an optional iterable of key-value pairs:
new Map([<iterable>])
| Parameter | Description |
|---|---|
iterable |
An optional iterable, such as an array or another Map, whose elements are two-element [key, value] arrays. Later pairs with a repeated key overwrite earlier ones |
The constructor returns a new Map. The new keyword is required, and calling Map() without it throws a TypeError.
Key Static Methods and Properties
| Member | Kind | Description |
|---|---|---|
Map.groupBy() |
Static method | Groups the elements of an iterable into a Map of arrays, keyed by what a callback returns (ES2024) |
Key Instance Methods and Properties
| Member | Kind | Description |
|---|---|---|
.get(), .has() |
Instance method | Return the value for a key, or undefined, and check whether a key is present |
.set() |
Instance method | Adds or updates an entry and returns the map, so calls can be chained |
.getOrInsert() |
Instance method | Returns the value for a key, first inserting a default value when the key is missing (ES2026) |
.getOrInsertComputed() |
Instance method | Returns the value for a key, first inserting the result of a callback when the key is missing (ES2026) |
.delete(), .clear() |
Instance method | Remove one entry and return whether it existed, or remove every entry |
.size |
Instance property | Returns the number of entries |
.keys(), .values(), .entries() |
Instance method | Return iterators over the keys, the values, or [key, value] pairs in insertion order |
.forEach() |
Instance method | Calls a function once per entry, in insertion order |
A Map is iterable, so for...of and spread syntax walk its [key, value] pairs in insertion order.
.getOrInsert() and .getOrInsertComputed() were added in ES2026 and are Baseline Newly available since 2026. The rest of Map is Baseline Widely available.
Examples
A blog’s tag cloud needs a count for each tag across many posts. .getOrInsert() returns the current count, or inserts 0 the first time a tag appears:
> const tagCounts = new Map();
undefined
> const tags = ["js", "css", "js", "html", "js"];
undefined
> for (const tag of tags) {
… tagCounts.set(tag, tagCounts.getOrInsert(tag, 0) + 1);
… }
Map(3) { 'js' => 3, 'css' => 1, 'html' => 1 }
> [...tagCounts.keys()]
[ 'js', 'css', 'html' ]
The keys come back in the order each tag first appeared.
An order dashboard splits orders into one list per status. Map.groupBy() builds the groups in one call:
> const orders = [
… { id: 1, status: "shipped" },
… { id: 2, status: "pending" },
… { id: 3, status: "shipped" },
… ];
undefined
> Map.groupBy(orders, (order) => order.status).get("shipped")
[ { id: 1, status: 'shipped' }, { id: 3, status: 'shipped' } ]
Common Gotchas
Bracket assignment sets an ordinary property on the map object instead of adding an entry, so .get() and .size don’t see it:
> const settings = new Map();
undefined
> settings["theme"] = "dark";
'dark'
> settings.get("theme")
undefined
> settings.size
0
JSON.stringify() doesn’t serialize entries and turns any map into "{}". Converting the map with Object.fromEntries() first keeps entries whose keys are strings:
> const prefs = new Map([["theme", "dark"]]);
undefined
> JSON.stringify(prefs)
'{}'
> JSON.stringify(Object.fromEntries(prefs))
'{"theme":"dark"}'
Related Resources
Course
JavaScript Map and Set for Pythonistas
Port a community garden's season report to JavaScript, and learn when to use a Map, a Set, or a plain object, and how to count, group, and compare without Counter or defaultdict.
For additional information on related topics, take a look at the following resources:
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Leodanis Pozo Ramos • Updated Nov. 19, 2026