--- title: Map slug: Web/JavaScript/Reference/Global_Objects/Map page-type: javascript-class browser-compat: javascript.builtins.Map sidebar: jsref --- The **`Map`** object holds key-value pairs and remembers the original insertion order of the keys. Any value (both objects and {{Glossary("Primitive", "primitive values")}}) may be used as either a key or a value. {{InteractiveExample("JavaScript Demo: Map", "taller")}} ```js interactive-example const map = new Map(); map.set("a", 1); map.set("b", 2); map.set("c", 3); console.log(map.get("a")); // Expected output: 1 map.set("a", 97); console.log(map.get("a")); // Expected output: 97 console.log(map.size); // Expected output: 3 map.delete("b"); console.log(map.size); // Expected output: 2 ``` ## Description `Map` objects are collections of key-value pairs. A key in the `Map` **may only occur once**; it is unique in the `Map`'s collection. A `Map` object is iterated by key-value pairs — a {{jsxref("Statements/for...of", "for...of")}} loop returns a 2-member array of `[key, value]` for each iteration. Iteration happens in _insertion order_, which corresponds to the order in which each key-value pair was first inserted into the map by the [`set()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/set) method (that is, there wasn't a key with the same value already in the map when `set()` was called). The specification requires maps to be implemented "that, on average, provide access times that are sublinear on the number of elements in the collection". Therefore, it could be represented internally as a hash table (with O(1) lookup), a search tree (with O(log(N)) lookup), or any other data structure, as long as the complexity is better than O(N). ### Key equality Value equality is based on the [SameValueZero](/en-US/docs/Web/JavaScript/Guide/Equality_comparisons_and_sameness#same-value-zero_equality) algorithm. (It used to use [SameValue](/en-US/docs/Web/JavaScript/Guide/Equality_comparisons_and_sameness#same-value_equality_using_object.is), which treated `0` and `-0` as different. Check [browser compatibility](#browser_compatibility).) This means {{jsxref("NaN")}} is considered the same as `NaN` (even though `NaN !== NaN`) and all other values are considered equal according to the semantics of the `===` operator. Also, for object keys, equality is based on object identity. They are compared by reference, not by value. See [Using the Map object](#using_the_map_object) for examples. ### Objects vs. Maps {{jsxref("Object")}} is similar to `Map`—both let you set keys to values, retrieve those values, delete keys, and detect whether something is stored at a key. For this reason (and because there were no built-in alternatives), `Object` has been used as `Map` historically. However, there are important differences that make `Map` preferable in some cases:
| Map | Object | |
|---|---|---|
| Accidental Keys |
A Map does not contain any keys by default. It only
contains what is explicitly put into it.
|
An Note: This can be bypassed by using {{jsxref("Object.create", "Object.create(null)")}}, but this is seldom done. |
| Security |
A Map is safe to use with user-provided keys and values.
|
Setting user-provided key-value pairs on an |
| Key Types |
A Map's keys can be any value (including functions,
objects, or any primitive).
|
The keys of an Object must be either a
{{jsxref("String")}} or a {{jsxref("Symbol")}}.
|
| Key Order |
The keys in |
Although the keys of an ordinary
The order was first defined for own properties only in ECMAScript
2015; ECMAScript 2020 defines order for inherited properties as well.
But note that no single mechanism
iterates
all of an object's properties; the various mechanisms
each include different subsets of properties.
({{jsxref("Statements/for...in", "for-in")}}
includes only enumerable string-keyed properties;
{{jsxref("Object.keys")}} includes only own, enumerable,
string-keyed properties;
{{jsxref("Object.getOwnPropertyNames")}} includes own,
string-keyed properties even if non-enumerable;
{{jsxref("Object.getOwnPropertySymbols")}} does the same
for just |
Size |
The number of items in a Map is easily retrieved from its
{{jsxref("Map.prototype.size", "size")}} property.
|
Determining the number of items in an Object is more roundabout and less efficient. A common way to do it is through the {{jsxref("Array/length", "length")}} of the array returned from {{jsxref("Object.keys()")}}.
|
| Iteration |
A Map is an
iterable, so it can be directly iterated.
|
Note:
|
| Performance |
Performs better in scenarios involving frequent additions and removals of key-value pairs. |
Not optimized for frequent additions and removals of key-value pairs. |
| Serialization and parsing |
No native support for serialization or parsing.
(But you can build your own serialization and parsing support for
|
Native support for serialization from {{jsxref("Object")}} to JSON, using {{jsxref("JSON.stringify()")}}. Native support for parsing from JSON to {{jsxref("Object")}}, using {{jsxref("JSON.parse()")}}. |