Home JavaScript Map Methods forEach() JavaScript Map forEach() Method Definition map.forEach(callbackFn) runs a function once for every entry , in insertion order. The callback receives (value, key, map): the value comes first , then the key (the same order as Array forEach, and the opposite of some other APIs). An optional second argument thisArg becomes this inside a regular function callback. The method returns undefined. Keys can be any value (SameValueZero).
map.forEach((value, key, map) => { ... })
value → first argument
key → second argument
map → the Map being looped
optional thisArg for regular functions
returns undefined
cannot break early Syntax Call it on a Map and pass a function. You can also pass a second argument that becomes this inside a regular function.
map.forEach(callbackFn)
map.forEach(callbackFn, thisArg) Call What it does map.forEach(callbackFn)Run callbackFn(value, key, map) for every entry. map.forEach(callbackFn, thisArg)Also set this inside a regular function callback. for (const [k, v] of map) { ... }A loop that does the same job, and it allows break.
Built into JavaScript since ES2015. Remember: value first, then key. For a loop you can stop early, see entries() with for...of.
Library declarations map.forEach(callbackFn)
map.forEach(callbackFn, thisArg)
// callbackFn(value, key, map)
// thisArg: optional
// returns: undefined Parameters Parameter Description callbackFn Required. Called once per entry with value, then key, then the map. A TypeError is thrown if it is not a function. thisArg Optional. Used as this when running a regular function callback. It has no effect on an arrow function.
Return Value Return Value Description undefinedNothing is returned. Collect results yourself in a variable inside the callback.
Important
You cannot stop forEach() early with break. Use for...of when you need to stop. Also watch the argument order: it is (value, key), not (key, value). Arrow functions ignore thisArg.
JavaScript Examples 1. Print pairs 2. Sum values 3. thisArg Example 1 — Print Every Key and Value Log each pair and confirm the callback order is value first, then key.
const ages = new Map();
ages.set("Ann", 31);
ages.set("Bob", 27);
ages.forEach(function (value, key) {
console.log(key + " is " + value);
});
const result = ages.forEach(function () {});
console.log(String(result)); // undefined Ann is 31
Bob is 27
undefined How It Works One call per entry. The callback runs twice, once for Ann and once for Bob, in insertion order.Value first, then key. The parameters are named value then key, so the log line uses them in that order.No return value. forEach() always returns undefined.Example 2 — Add Up the Values Keep a running total in a variable because forEach() returns nothing.
const scores = new Map();
scores.set("Ann", 10);
scores.set("Bob", 20);
scores.set("Carl", 5);
let total = 0;
scores.forEach(function (value) {
total += value;
});
console.log(total); // 35 How It Works Start a total outside. total lives outside the callback so each call can update it.Only the value is needed. The callback uses the first argument and ignores the key.The sum is 35. 10 + 20 + 5 are added in insertion order.Example 3 — Pass thisArg to a Regular Function A second argument becomes this inside a regular function callback.
const scores = new Map();
scores.set("Ann", 10);
scores.set("Bob", 20);
const box = { sum: 0 };
scores.forEach(function (value) {
this.sum += value;
}, box);
console.log(box.sum); // 30 How It Works thisArg is the second argument. box is passed after the function.this points at box. Inside the regular function, this.sum updates the object.Arrow functions ignore thisArg. This example uses function on purpose so this works.Compatibility
Browser & Runtime Support Map.prototype.forEach() is part of ES2015 (ES6), together with keys(), values(), and entries().
✓ Baseline · ES2015
Map.prototype.forEach() Works in every modern browser, plus Node.js, Deno, and Bun.
98% Modern browser support
Google Chrome 36+ · Desktop & Mobile
Full support
Mozilla Firefox 6+ · Desktop & Mobile
Full support
Apple Safari 8+ · macOS & iOS
Full support
Microsoft Edge 12+
Full support
Node.js 0.12+ · every current release
Supported
Deno All releases
Supported
Internet Explorer IE 11 · no constructor argument
Basic Map.prototype.forEach() Excellent
Bottom line: Safe to use in any modern browser and runtime. forEach() has been part of Map since ES2015.
Frequently Asked Questions What is the argument order of Map forEach()? The callback is (value, key, map). Value comes first, then key. That matches Array forEach and differs from some other APIs.
Does Map forEach() return anything? No. It returns undefined. Keep totals or lists in variables you create outside the callback.
Can I break out of forEach early? No. break does not work inside the callback. Use for...of over the Map when you need to stop.
What does thisArg do? It sets this inside a regular function callback. Arrow functions ignore thisArg because they keep their outer this.
When should I use forEach instead of for...of? Use forEach when a short callback is enough and you do not need break. Prefer for...of when you want early exit or await.
CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
11 people found this page helpful
Helpful Share Copy link Suggestion