References

Beginner-friendly references for web development, with live, editable examples.

The JavaScript Map object

Object JavaScript All modern browsers Updated
Quick answer

A Map is a key-value collection where keys can be any type (not just strings), and the insertion order is remembered. Create one with new Map(), then use set(key, value), get(key), has(key), delete(key) and the size property. It's often a better fit than a plain object for dynamic key-value data.

Overview

A Map stores key-value pairs, like an object, but with real advantages for certain jobs. Its keys can be any type — objects, functions, numbers, not just strings — and it keeps pairs in insertion order, which iteration respects. You build one with new Map() and work with it through methods rather than property access.

The core API is small and clear: map.set(key, value) adds or updates, map.get(key) reads, map.has(key) checks existence, map.delete(key) removes, and map.size counts (a property, not a method like an array's length). Because set() returns the map, you can chain calls. To loop, a Map is iterable, so for...of gives you [key, value] pairs directly — no Object.entries() needed.

When should you pick a Map over an object? When keys aren't strings, when you add and remove entries frequently, when order matters, or when you want a reliable size. Objects are still better for fixed, record-like data and for anything you'll serialize to JSON (Map doesn't stringify directly). Its set-based cousin is Set, for unique values without values attached.

Syntax

const m = new Map();
m.set("name", "Ada");
m.set(42, "answer");      // non-string key

m.get("name");           // "Ada"
m.has(42);               // true
m.size;                  // 2

for (const [key, value] of m) { /* ... */ }

Example

Live example
<pre id="out" style="font:15px ui-monospace,monospace"></pre>
<script>
  const scores = new Map();
  scores.set('Ada', 95).set('Linus', 88);

  const lines = [];
  for (const [name, score] of scores) {
    lines.push(name + ': ' + score);
  }
  lines.push('size: ' + scores.size);

  document.getElementById('out').textContent = lines.join('\n');
</script>

Best practices

  • Use a Map when keys aren't strings, when you add/remove often, or when order and a reliable size matter.
  • Loop directly with for...of — it yields [key, value] pairs.
  • Use a plain object for fixed records and anything you'll convert to JSON.
  • Initialize from pairs: new Map([["a", 1], ["b", 2]]).

Frequently asked questions

What is the difference between a Map and an object?
A Map allows any key type, preserves insertion order, has a size property, and is built for frequent additions and deletions. An object uses string/symbol keys and suits fixed records and JSON.
How do I get the number of entries in a Map?
Read the size property: map.size (it's a property, not a method).
How do I loop over a Map?
Use for (const [key, value] of map) { ... }, or map.forEach().
Can I convert a Map to JSON?
Not directly — JSON.stringify() ignores Map entries. Convert with Object.fromEntries(map) first (string keys only).