JavaScript

JavaScript Map forEach() Method

The Map forEach() method in Javascript is used to go through a map’s items and execute a function for every item in the map. An item in the map is nothing but a key-value pair. From this, it is easy to conclude that forEach() runs a function for every key-value pair. The forEach() method is applied to a map variable with the help of a dot operator. First, observe the syntax of the forEach() method.

Syntax of forEach() method

The syntax of the forEach() method is given as :

mapVar.forEach(callbackFunction, key, value, this)
  • mapVar: The map variable on which the forEach() is applied
  • callbackFunction: The callback function to be executed for every entry inside the map
  • key: The key of the key-value pairs inside the map variable for the call back function to use
  • value: The value of the key-value pairs inside the map variable for the call back function to use
  • this: It is used to set the “this” reference for the callback function

Return Value
The return value of the forEach() method is always undefined

Example 1: Printing the keys values of the map on the terminal

To demonstrate the working of the forEach() method, first create a new map by using the following lines of code

let mapVar = new Map();
mapVar.set("Paris", 1);
mapVar.set("Rome", 2);
mapVar.set("London", 3);
mapVar.set("Rio", 4);

In the code mentioned above, a map was created using the first line and then mapVar.set() function populated the map with key and value pairs.

After that, simply apply the forEach() method on the mapVar and print out the key-value pairs onto the terminal using the following line of code

mapVar.forEach((keys, values) => {
  console.log("\n" + keys + " " + values);
});

After that, simply execute the code and observe the following output onto the terminal

The keys and the respective values were printed onto the terminal.

Example 2: Filter a specific value from the forEach() callback function.

The user can easily apply a filter to the key-value pair inside the forEach() method to stop the execution of the call-back function for a specific key or value. For this, simply wrap the statements of the callback function with the if condition.

Create a map using the same lines of code as the previous example

let mapVar = new Map();

mapVar.set("Paris", 1);
mapVar.set("Rome", 2);
mapVar.set("London", 3);
mapVar.set("Rio", 4);

After that, apply the forEach() function onto the mapVar using the following lines of code

mapVar.forEach((keys, values) => {
  //Coming lines will be places here
});

After that, inside the callback function, use an if-condition to filter the value Paris from the output and print all other values onto the terminal

 if (values !== "Paris") {
    console.log("\n" + keys + " " + values);
 }

The whole code snippet for applying the forEach() method will look like this

mapVar.forEach((keys, values) => {
  if (values !== "Paris") {
    console.log("\n" + keys + " " + values);
  }
});

Executing this program, will yield the following output on the terminal

It is clear from the output, that the “Paris” value was excluded or filtered out from the output.

Example 3: Checking the return value of the forEach() method

For this, simply take a map from the previous example like

let mapVar = new Map();

mapVar.set("Paris", 1);
mapVar.set("Rome", 2);
mapVar.set("London", 3);
mapVar.set("Rio", 4);

After that, apply the forEach() method to the mapVar assign the whole statement to a new variable

var resultValue = mapVar.forEach((keys, values) => {
  console.log(keys, values);
});

After this, print out the value inside the resultValue variable by using the console log function

console.log("\nThe return value from forEach() is as : " + resultValue);

Execute the program and observe the output as

It is crystal clear from the result on the terminal that the forEach() method returns undefined

Conclusion

The Map forEach() method goes through every key-pair value pair in a map variable and executes a callback function for every key-value pair. With the help of if-else statements, a check can be applied to not perform a function upon encountering a specific value or key. To use the forEach() method, you need to apply it to a map object by using a dot operator. And after its complete execution, it returns an undefined value to the caller (if there is one).

About the author

Abdul Mannan

I am curious about technology and writing and exploring it is my passion. I am interested in learning new skills and improving my knowledge and I hold a bachelor's degree in computer science.