JavaScript Map forEach()
Syntax & Examples


Map.forEach() method

The forEach() method of the Map object in JavaScript calls a provided callback function once for each key-value pair present in the Map object, in insertion order. If a thisArg parameter is provided to forEach, it will be used as the this value for each callback.


Syntax of Map.forEach()

There are 2 variations for the syntax of Map.forEach() method. They are:

1.
forEach(callbackFn)

Parameters

ParameterOptional/RequiredDescription
callbackFnrequiredFunction to execute for each element, taking three arguments: value, key, and the Map object being traversed.

This method calls callbackFn once for each key-value pair present in the Map object, in insertion order.

Returns value of type undefined.

2.
forEach(callbackFn, thisArg)

Parameters

ParameterOptional/RequiredDescription
callbackFnrequiredFunction to execute for each element, taking three arguments: value, key, and the Map object being traversed.
thisArgoptionalValue to use as this when executing callbackFn.

This method calls callbackFn once for each key-value pair present in the Map object, in insertion order. thisArg parameter will be used as the this value for each callback.

Returns value of type undefined.



✐ Examples

1 Using forEach() to log key-value pairs

In JavaScript, we can use the forEach() method to log each key-value pair in a Map object.

For example,

  1. Create a new Map object map with initial key-value pairs.
  2. Use the forEach() method to log each key-value pair in the map to the console.

JavaScript Program

const map = new Map([['a', 1], ['b', 2], ['c', 3]]);
map.forEach((value, key) => {
  console.log(`${key}: ${value}`);
});

Output

a: 1
b: 2
c: 3

2 Using forEach() with a thisArg parameter

In JavaScript, we can use the forEach() method with a thisArg parameter to log each key-value pair in a Map object with a specific this context.

For example,

  1. Create a new Map object map with initial key-value pairs.
  2. Create an object logger with a prefix property.
  3. Use the forEach() method with a callback function that logs each key-value pair in the map to the console, using logger as the this context.

JavaScript Program

const map = new Map([['a', 1], ['b', 2], ['c', 3]]);
const logger = { prefix: 'Key-Value Pair:' };
map.forEach(function(value, key) {
  console.log(`${this.prefix} ${key}: ${value}`);
}, logger);

Output

Key-Value Pair: a: 1
Key-Value Pair: b: 2
Key-Value Pair: c: 3

3 Using forEach() to modify values in a Map

In JavaScript, we can use the forEach() method to modify each value in a Map object.

For example,

  1. Create a new Map object map with initial key-value pairs.
  2. Use the forEach() method to multiply each value in the map by 2.
  3. Log the modified map to the console.

JavaScript Program

const map = new Map([['a', 1], ['b', 2], ['c', 3]]);
map.forEach((value, key, map) => {
  map.set(key, value * 2);
});
console.log(map);

Output

Map { 'a' => 2, 'b' => 4, 'c' => 6 }

Summary

In this JavaScript tutorial, we learned about forEach() method of Map: the syntax and few working examples with output and detailed explanation for each example.