Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Map.prototype.forEach

Calls a function for each entry of a Map.

Available since: ES2015

Syntax

map.forEach(callbackFn)

Parameters

ParameterTypeDescription
callbackFnFunctionA function that receives the value, the key and the Map.

Return value

undefinedReturns nothing.

Examples

Note the order of the arguments:

const ages = new Map([['Anna', 25], ['Oleg', 31]]);
 
ages.forEach((age, name) => {
  console.log(name, age);
});

Output:

Anna 25
Oleg 31

Notes

The value comes first and the key second, like an array's forEach() (element, index). A for...of loop with destructuring reads more clearly.

See also

In the tutorial

  • Map and Set — The Map and Set collections in JavaScript: a dictionary with keys of any type and a set of unique values.

Thanks for reading!

Donate to the site — we work hard for you.