Map Since: ES6(ECMAScript 2015)
A collection that manages key-value pairs. Unlike objects, any data type can be used as a key, and insertion order is preserved.
Syntax
// Create a new Map. var map = new Map(); // You can also create one with initial values. var map = new Map([ ["key1", "value1"], ["key2", "value2"] ]);
Methods and Properties
| Method / Property | Description |
|---|---|
| set(key, value) | Adds a key-value pair. If the key already exists, its value is overwritten. |
| get(key) | Returns the value associated with the specified key. Returns undefined if the key does not exist. |
| has(key) | Returns true or false indicating whether the specified key exists. |
| delete(key) | Removes the pair with the specified key. Returns true on success, or false if the key did not exist. |
| clear() | Removes all key-value pairs. |
| size | Returns the number of key-value pairs stored in the Map. |
| forEach(function) | Executes a function for each key-value pair. The callback receives arguments in the order: value, key, Map itself. |
Sample Code
// Basic Map operations
var userMap = new Map();
// Add entries.
userMap.set("name", "Taro");
userMap.set("age", 25);
userMap.set("active", true);
// Retrieve values.
console.log(userMap.get("name")); // Outputs "Taro".
console.log(userMap.get("age")); // Outputs 25.
// Check if a key exists.
console.log(userMap.has("name")); // Outputs true.
console.log(userMap.has("email")); // Outputs false.
// Check the number of entries.
console.log(userMap.size); // Outputs 3.
// Iterate over all pairs in insertion order.
userMap.forEach(function(value, key) {
console.log(key + ": " + value);
});
// Using objects as keys.
var key1 = { id: 1 };
var key2 = { id: 2 };
var objMap = new Map();
objMap.set(key1, "User 1");
objMap.set(key2, "User 2");
console.log(objMap.get(key1)); // Outputs "User 1".
// Delete an entry.
userMap.delete("active");
console.log(userMap.size); // Outputs 2.
// Remove all entries.
userMap.clear();
console.log(userMap.size); // Outputs 0.
Map vs. Object
| Feature | Map | Object |
|---|---|---|
| Key types | Any data type can be used. | Only strings and Symbols. |
| Insertion order | Always preserved. | Preserved, but numeric keys are sorted first. |
| Getting the count | Available via the size property. | Requires Object.keys(obj).length. |
| Iteration | Directly iterable with forEach() or for...of. | Requires conversion with Object.keys() or similar. |
Overview
Map is a collection that stores key-value pairs, introduced as a data structure in ES6. While plain objects can also manage key-value pairs, Map offers several advantages: any data type can be used as a key, insertion order is always preserved, and the count is easily accessible via the size property.
Map is particularly well-suited when you need to use objects or functions as keys, or when you frequently add and remove entries. Note that the callback for forEach() receives arguments in the order value, key, Map itself. This matches the behavior of Array.forEach(), where the value is the first argument — so be careful not to confuse the order.
If you only need to store unique values, use Set instead.
Browser Compatibility
37 or earlier ×
12 or earlier ×
7 or earlier ×
Android Browser
43+ ○
37 or earlier ×
Chrome Android
43+ ○
37 or earlier ×
Firefox Android
79+ ○
13 or earlier ×If you find any errors or copyright issues, please contact us.