JavaScript Map forEach() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Map

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).

Remember
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.

JavaScript
map.forEach(callbackFn)
map.forEach(callbackFn, thisArg)
CallWhat 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
JavaScript
map.forEach(callbackFn)
map.forEach(callbackFn, thisArg)

// callbackFn(value, key, map)
// thisArg: optional
// returns: undefined

Parameters

ParameterDescription
callbackFnRequired. Called once per entry with value, then key, then the map. A TypeError is thrown if it is not a function.
thisArgOptional. Used as this when running a regular function callback. It has no effect on an arrow function.

Return Value

Return ValueDescription
undefinedNothing is returned. Collect results yourself in a variable inside the callback.

JavaScript Examples

Example 1 — Print Every Key and Value

Log each pair and confirm the callback order is value first, then key.

JavaScript
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
Try It Yourself

How It Works

  1. One call per entry. The callback runs twice, once for Ann and once for Bob, in insertion order.
  2. Value first, then key. The parameters are named value then key, so the log line uses them in that order.
  3. No return value. forEach() always returns undefined.

Example 2 — Add Up the Values

Keep a running total in a variable because forEach() returns nothing.

JavaScript
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
Try It Yourself

How It Works

  1. Start a total outside. total lives outside the callback so each call can update it.
  2. Only the value is needed. The callback uses the first argument and ignores the key.
  3. 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.

JavaScript
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
Try It Yourself

How It Works

  1. thisArg is the second argument. box is passed after the function.
  2. this points at box. Inside the regular function, this.sum updates the object.
  3. Arrow functions ignore thisArg. This example uses function on purpose so this works.

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

The callback is (value, key, map). Value comes first, then key. That matches Array forEach and differs from some other APIs.
No. It returns undefined. Keep totals or lists in variables you create outside the callback.
No. break does not work inside the callback. Use for...of over the Map when you need to stop.
It sets this inside a regular function callback. Arrow functions ignore thisArg because they keep their outer this.
Use forEach when a short callback is enough and you do not need break. Prefer for...of when you want early exit or await.

Next: getOrInsert()

Need to stop a loop early? Switch to entries() with for...of.

getOrInsert() tutorial →

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.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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