JavaScript Map Object
A Map holds key-value pairs where keys can be any type, including objects, and remembers the order they were added. It has methods to get, set, check and delete entries.
Description
A Map is a collection of key-value pairs. Unlike a plain object, its keys can be any value, including objects, functions and numbers, without being converted to strings. Entries keep their insertion order, the number of entries is available as size, and it is directly iterable with for...of.
Use set(), get(), has() and delete() to work with entries. Keys are compared with the SameValueZero algorithm, so NaN can be a key and two different objects are different keys even if they look the same. Maps are a better choice than objects for collections that are added to and removed from often. To convert to an object, use Object.fromEntries(map).
Unlike objects, maps have no inherited keys, so there's no risk of a key such as "constructor" clashing with a built-in property. Maps are generally faster for frequent additions and removals. The trade-off is that they don't convert to JSON directly: JSON.stringify(map) gives {}, so convert with Object.fromEntries() or [...map] first. Map.groupBy() groups items into a map in one step.
Creating
new Map()
new Map([[key1, value1], [key2, value2]])Static methods
| Name | Description |
|---|---|
Map.groupBy() | The Map.groupBy() static method groups the items of an iterable into a Map, using keys of any type returned by a function. |
Instance properties
| Name | Description |
|---|---|
Map.prototype.size | The size property gives the number of entries in a Map. |
Instance methods
| Name | Description |
|---|---|
Map.prototype.clear() | The clear() method removes every entry from a Map. |
Map.prototype.delete() | The delete() method removes an entry from a Map by its key. |
Map.prototype.entries() | The entries() method returns an iterator of a Map's [key, value] pairs in insertion order. |
Map.prototype.forEach() | The forEach() method runs a function for each entry of a Map, in insertion order. |
Map.prototype.get() | The get() method returns the value stored for a key in a Map, or undefined. |
Map.prototype.has() | The has() method checks whether a Map contains a key. |
Map.prototype.set() | The set() method adds or updates an entry in a Map and returns the Map, so calls can be chained. |
Example
Browser Support
The following information will show you the current browser support for the Map object. Hover over a browser icon to see the version that first introduced support for this object.
Desktop





Tablets & Mobile





