Map (initial support)
Map stores key-value pairs while preserving insertion order. Unlike plain objects, it accepts any value as a key and exposes a predictable iteration model.
Overview
Map stores key-value pairs while preserving insertion order. Unlike plain objects, it accepts any value as a key and exposes a predictable iteration model.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 38 | 12 | 13 | 8 | 38 | 8 | |
| The [Symbol.iterator]() method of Map instances implements the iterable protocol and allows Map objects to be consumed by most syntaxes expecting iterables, such as the spread syntax and Statements/for...of loops. It returns a map iterator object that yields the key-value pairs of the map in insertion order. | 38 | 12 | 36 | 10 | 38 | 10 |
| The Map[Symbol.species] static accessor property is an unused accessor property specifying how to copy Map objects. | 51 | 13 | 41 | 10 | 51 | 10 |
| The Map() constructor creates Map objects. | 38 | 12 | 13 | 8 | 38 | 8 |
Map.Map.iterable allowed `new Map(iterable)` | 38 | 12 | 13 | 9 | 38 | 9 |
Map.Map.null allowed `new Map(null)` | 38 | 12 | 37 | 9 | 38 | 9 |
| The clear() method of Map instances removes all elements from this map. | 38 | 12 | 19 | 8 | 38 | 8 |
| The delete() method of Map instances removes the entry specified by the key from this Map. | 38 | 12 | 13 | 8 | 38 | 8 |
| The entries() method of Map instances returns a new map iterator object that contains the [key, value] pairs for each element in this map in insertion order. | 38 | 12 | 20 | 8 | 38 | 8 |
| The forEach() method of Map instances executes a provided function once per each key/value pair in this map, in insertion order. | 38 | 12 | 25 | 8 | 38 | 8 |
| The get() method of Map instances returns the value corresponding to the key in this Map, or undefined if there is none. Object values are returned as the same reference that was originally stored, not as a copy, so mutations to the returned object will be reflected anywhere that reference is held, including inside the Map. | 38 | 12 | 13 | 8 | 38 | 8 |
| The has() method of Map instances returns a boolean indicating whether an entry with the specified key exists in this Map or not. | 38 | 12 | 13 | 8 | 38 | 8 |
Map.key equality for zeros Key equality for -0 and 0 | 38 | 12 | 29 | 9 | 38 | 9 |
| The keys() method of Map instances returns a new map iterator object that contains the keys for each element in this map in insertion order. | 38 | 12 | 20 | 8 | 38 | 8 |
| The set() method of Map instances adds a new entry with a specified key and value to this Map, or updates an existing entry if the key already exists. | 38 | 12 | 13 | 8 | 38 | 8 |
| The size accessor property of Map instances returns the number of elements in this map. | 38 | 12 | 19 | 8 | 38 | 8 |
| The values() method of Map instances returns a new map iterator object that contains the values for each element in this map in insertion order. | 38 | 12 | 20 | 8 | 38 | 8 |
- This feature was removed in a later browser version (36)
- This feature was removed in a later browser version (27)
- Previously available under a different name: @@iterator (27)
- A placeholder property named `@@iterator` is used.
- Previously available under a different name: iterator (17)
- A placeholder property named `iterator` is used.
- From Firefox 13 to Firefox 18, the `size` property was implemented as a `Map.prototype.size()` method, this has been changed to a property in later versions conform to the ECMAScript 2015 specification.
Syntax
const map = new Map();
map.set('name', 'Taro');
map.set(42, 'the answer');
map.set(true, 'boolean key');
map.get('name'); // 'Taro'
map.has(42); // true
map.size; // 3
// Iteration
for (const [key, value] of map) {
console.log(`${key}: ${value}`);
} Live demo
Basic Map operations
Add, read, delete, and iterate over entries while watching insertion order stay intact.
Object keys by reference
Map treats each object reference as a distinct key, even when the contents look the same.
Use cases
Caching by identity
Use Map when objects, numbers, or booleans need to work as keys without string conversion.
Counting and grouping
Map works well for frequency tables, grouped records, and lookup tables built at runtime.
Cautions
- Map compares object keys by reference, not by their shape or contents.
- If you only need string keys and JSON serialization, a plain object can still be the simpler choice.
Accessibility
- When Map-backed data changes what users see, update a visible status message or aria-live region instead of changing the UI silently.
Related links
Powered by web-features