Keyed lookup
Associate metadata or computed results with object, function, or primitive keys without converting keys to strings.
JavaScript · Class
Map stores unique key-value pairs and preserves key insertion order. Any JavaScript value can be used as a key or a value.
A Map stores unique key-value pairs and preserves the keys' insertion order during iteration. Keys and values can be any JavaScript values; use methods such as set(), get(), has(), and delete() to modify or query entries.
new Map()
new Map(iterable)| Name | Type | Description |
|---|---|---|
iterable | Iterable | An optional iterable of key-value pairs used to initialize the new Map. |
A new Map object.
const cache = new Map([
["language", "JavaScript"],
["version", 2024],
]);
cache.set("runtime", "Node.js");
console.log(cache.get("language"));
console.log(cache.has("version"));
console.log(cache.size);
for (const [key, value] of cache) {
console.log(`${key}: ${value}`);
}Creates a Map from iterable key-value pairs, adds an entry, reads values, checks membership, reports size, and iterates in insertion order.
Associate metadata or computed results with object, function, or primitive keys without converting keys to strings.
Maintain an insertion-ordered collection of entries for traversal or serialization.
Track membership and retrieve the number of stored entries with dedicated collection operations.
Map stores key-value pairs, allows any value as a key, and preserves key insertion order. Each key is unique, so assigning an existing key updates its associated value.
Follow-up: How do object keys compare? They compare by reference, so separately created but structurally identical objects do not refer to the same Map entry.
The constructor accepts an iterable whose elements are key-value pairs and creates a new Map. With no argument, it creates an empty Map.
Follow-up: How would you initialize a Map with existing data? Pass an iterable such as an array of two-element arrays to new Map().
Map is directly iterable, and iteration produces key-value pairs in insertion order. The size property gives the number of entries without manually counting them.
Follow-up: Which common loop form consumes those pairs? Use for (const [key, value] of map) to destructure each two-element pair.
Map.prototype.set()Map.prototype.get()Map.prototype.has()Map.prototype.delete()Map.prototype.clear()Map.prototype.entries()Map.prototype.keys()Map.prototype.values()