In JavaScript, a Map stores key / value pairs. Keys can be any type — strings, numbers, objects, NaN, and more. Entries keep insertion order. You can read how many there are with map.size, and you can loop with for...of or forEach().
You create one with new Map(), then use set(), get(), has(), and delete(). Unlike a plain object, a Map does not treat string keys as property names, and it does not inherit from Object.prototype.
A Map is the built-in collection when you need key/value data that you can count and iterate in a stable order.
Syntax
JavaScript Syntax
Common ways to create a Map and use it:
JavaScript
const map = new Map(); // empty
map.set("id", 1); // store (returns map)
map.get("id"); // 1
map.has("id"); // true
map.delete("id"); // true
map.size; // 0
const filled = new Map([["a", 1], ["b", 2]]);
for (const [k, v] of filled) {
// insertion order: "a" then "b"
}
Keys
Keys and SameValueZero
Map keys can be anything: strings, numbers, objects, arrays, functions, NaN, null, and undefined. Keys are compared with SameValueZero: NaN equals NaN, and +0 and -0 count as the same key. Object keys match by identity — two different objects with the same shape are two different keys.
That is different from a plain object, where every key becomes a string (or symbol), and different from a WeakMap, which only allows object (and local symbol) keys and cannot be listed.
Use cases
When to Use a Map
Use
How it works
Why Map fits
Non-string keys
Key by an object, a number, or NaN
Objects coerce keys to strings; Map does not
Frequent add / remove
Update a lookup table as data arrives
size, delete(), and clear() are built in
Preserve order
Walk entries in the order you inserted them
Insertion order is guaranteed
Avoid prototype surprises
Do not inherit toString or other Object keys
A Map is not an object bag of properties
Compare
Map vs WeakMap
Question
Map
WeakMap
Allowed keys
Any value
Objects (and local symbols) only
Keeps keys alive?
Yes, until you delete them
No, keys can be cleaned up
Size
map.size
None
Loop over entries
for...of, forEach(), keys()
Not possible
Empty it
clear()
Create a new WeakMap
Best for
Data you want to list, count, or show
Hidden extra data about objects
Newer
Newer Upsert Methods
getOrInsert() and getOrInsertComputed() read a value, and store one first if the key is missing. They were added to Map and WeakMap together and replace the long if (!map.has(k)) map.set(k, v) pattern.
They are not in every engine yet. We tested Node.js 24.15, which does not have them, and Chrome 148, which does. Check before you rely on them: typeof Map.prototype.getOrInsert === "function". Their examples include a tiny fallback so they still run everywhere.
Code
JavaScript Examples
Three short programs that show how a Map works.
Example 1 — Store, Read, Check, and Remove
Create a Map and walk through set, get, has, delete, and size.
A Map is a collection of key and value pairs. Keys can be any type: strings, numbers, objects, functions, and even NaN. You create one with new Map(), add pairs with set(), read with get(), and check size with the size property.
Object keys are mostly strings or symbols. Map keys can be any value, including objects. Map also keeps insertion order, has a size property, and is built for frequent add and remove. Prefer Map when keys are not plain string names.
Map accepts any key, keeps keys alive, has size, and can be looped over. WeakMap accepts only objects as keys, does not keep them alive, and cannot be counted or iterated. Choose Map when you need to list or count entries.
Use for...of on the Map itself to get each [key, value] pair. You can also use map.keys(), map.values(), map.entries(), or map.forEach(). Entries follow insertion order.
The main ones are set, get, has, delete, and clear. You also get size, keys, values, entries, and forEach. Newer helpers like getOrInsert appear on some engines. Start with set, get, has, and size.