Map

Map.prototype.forEach

Executes a provided function once per each key/value pair in the Map object, in insertion order

Syntax

JavaScript
map.forEach(callbackFn, thisArg?)

Parameters

ParameterTypeDescription
callbackFn(value, key, map) => voidFunction to execute for each entry
thisArganyValue to use as this when executing callbackFn

Return Value

undefined

Examples

Basic Usage
const map = new Map([['a', 1], ['b', 2], ['c', 3]]);
map.forEach((value, key) => {
  console.log(`${key}: ${value}`);
});
// a: 1, b: 2, c: 3
Practical Example
const prices = new Map([['apple', 1.5], ['banana', 0.5]]);
let total = 0;
prices.forEach(price => { total += price; });
console.log(total); // 2
Advanced Usage
const map = new Map([['x', 10], ['y', 20]]);
const entries: string[] = [];
map.forEach((v, k) => entries.push(`${k}=${v}`));
console.log(entries.join('&')); // 'x=10&y=20'

Understanding Map.prototype.forEach

The Map.prototype.forEach method in JavaScript executes a provided function once per each key/value pair in the Map object, in insertion order. It belongs to the Map object and is one of the most widely used methods for working with map values in modern JavaScript and TypeScript applications.

The method signature is map.forEach(callbackFn, thisArg?). It accepts 2 parameters: callbackFn, thisArg. When called, it returns undefined. Understanding when and how to use forEach() helps you write more expressive, readable code.

Common use cases for Map.prototype.forEach include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like map-set, map-get, array-foreach, enabling you to chain operations together for complex data manipulation pipelines.

Browser support for Map.prototype.forEach is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.

Browser Compatibility

Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.

Related Methods

More Map Methods

Other methods in the Map object

Related Tools

More Map Methods

Explore JavaScript Methods

Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.