JavaScript Map Methods

About This Index
This page is the JavaScript Map Methods index for the tutorial track. Each row links to a full guide with syntax, three runnable examples, browser notes, and FAQs.
Learn what a Map is on /js/map. Use this page when you need a specific API such as set(), get(), or Map.groupBy().
Strings, numbers, objects, NaN, and more all work as keys. Keys are compared with SameValueZero (NaN equals NaN; +0 and -0 are the same).
How to Call a Map Method
Instance methods use a dot on the Map. The one static helper is Map.groupBy:
const map = new Map();
map.set("role", "admin"); // store a value (returns map)
map.get("role"); // "admin"
map.has("role"); // true
map.delete("role"); // true
map.clear(); // empty the Map
map.size; // 0
// Iterators (insertion order)
for (const [k, v] of map) { /* ... */ }
map.keys(); map.values(); map.entries();
map.forEach(function (value, key) { /* ... */ });
// Static helper (ES2024)
Map.groupBy(items, function (item) { return item.type; });
// Newer upsert methods: check support first
map.getOrInsert("role", "guest");
map.getOrInsertComputed("role", function (key) { return "guest"; }); JavaScript Map Methods Table
All method guides in this JavaScript tutorial series (12 tutorials), grouped by what they do.
Store and update
3 methodsWrite a value for a key. set() always writes; the two newer upsert methods write only when the key is missing.
| Method | Description | Tutorial |
|---|---|---|
set() | Store a value for a key. Returns the Map, so calls can be chained. Any key type works. | Open |
getOrInsert() | Read the value for a key, or store a default first when the key is missing (newer method). | Open |
getOrInsertComputed() | Read the value for a key, or call a function to build and store it when the key is missing (newer method). | Open |
Read
2 methodsLook a key up without changing the Map. Neither method throws for a missing key.
Remove
2 methodsDelete one entry, or empty the whole Map in one call.
Loop over a Map
4 methodsWalk keys, values, or entries in insertion order. Map is iterable, so for...of works too.
| Method | Description | Tutorial |
|---|---|---|
keys() | Get an iterator of the keys in the order they were added. | Open |
values() | Get an iterator of every value in the Map, in the order the keys were added. | Open |
entries() | Get an iterator of [key, value] pairs. This is also what for...of uses on a Map by default. | Open |
forEach() | Run a function once for every entry. The callback gets value first, then key. | Open |
Static helper
1 methodBuild a Map of groups from a list without creating the Map yourself first.
| Method | Description | Tutorial |
|---|---|---|
Map.groupBy() | Build a new Map that groups items from a list under keys your function chooses (static helper). | Open |
Map Constructor and Members
Here is how to create a Map and the members it has. Compare this with a WeakMap, which has no size, no clear(), and cannot be looped over.
| Member | What it gives you | Since |
|---|---|---|
new Map() | Create an empty Map. | ES2015 |
new Map([[k, v], ...]) | Create a Map that already holds some pairs. Keys can be any value. | ES2015 |
map.size | How many entries the Map holds right now. | ES2015 |
map.clear() | Remove every entry in one call. | ES2015 |
map.keys(), values(), entries(), forEach() | Walk the Map in insertion order. See the method guides linked above. | ES2015 |
for (const [k, v] of map) | Maps are iterable. for...of yields [key, value] pairs. | ES2015 |
Map.groupBy(items, callbackFn) | Static helper: build a Map of groups from a list (ES2024). | ES2024 |
Frequently Asked Questions
Start with set()
Learn how to store a value for any key, and why set() returns the Map for chaining.
CODETOFUN NEWS
Stay Ahead in Tech
Get the latest IT news, programming updates, and technology trends directly on your phone.
Your daily dose of tech, straight to your feed.
14 people found this page helpful
