Favorites

JavaScript Map Object

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
Map
JavaScript Keyed Collections · ConstructorThis object is supported by all modern browsers.
Updated 28th November 2026

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

NameDescription
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

NameDescription
Map.prototype.sizeThe size property gives the number of entries in a Map.

Instance methods

NameDescription
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

const prices = new Map([['tea', 3], ['cake', 5]]);
prices.set('coffee', 4);

console.log(prices.get('cake'));
console.log(prices.has('juice'), prices.size);

for (const [item, price] of prices) {
  console.log(item, price);
}

const keyObj = { id: 1 };
prices.set(keyObj, 'object key');
console.log(prices.get(keyObj));

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.

This object is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee