How to iterate a Map in Javascript?

Srijan } Author Pic
SrijanJan 29, 2025 - JavaScript
How to iterate a Map in Javascript? - Reacted Node

Photo: Kelly/Pexels

In this article, we will learn how to iterate over a Map using different methods available. So, let's dive in and learn how to loop over a Map.

If you missed reading the first article in which we discussed how to do basic operations over a Map, you can read it here.

Iterate a Map

For iterating over a Map, we can use the following javascript constructs:

  • for..of
  • forEach()

Let's create a Map first to loop over from the knowledge gained in our previous article.

1let map = new Map();
2
3map.set("one", "first element");
4map.set("two", "second element");
5map.set(3, "third element");

Iterating map with for..of

We can use for..of to iterate a Map in Javascript.

1for (let [key, value] of map) {
2 console.log(key + " = " + value);
3}
4
5//output
6// one = first element
7// two = second element
8// 3 = third element

Map also provides these three methods, which comes in handy while iterating over a Map.

  • map.keys() - Returns an iterable for keys
  • map.values() - Returns an iterable for values
  • map.entries() - Returns an iterable of [key,value]
1for (let key of map.keys()) {
2 console.log(key);
3}
4
5// output
6// one
7// two
8// 3
9
10for (let value of map.values()) {
11 console.log(value);
12}
13
14// output
15// first element
16// second element
17// third element
18
19for (let [key, value] of map.entries()) {
20 console.log(key + " = " + value);
21}
22
23//output
24// one = first element
25// two = second element
26// 3 = third element

Iterating Map with forEach()

We can also iterate through a Map using forEach().

1map.forEach(function (value, key) {
2 console.log(key + " = " + value);
3});
4
5//output
6// one = first element
7// two = second element
8// 3 = third element

Summary

  • To iterate over a Map, we can use for..of and forEach() loop constructs.
  • Map provides three methods that return iterable: map.keys(), map.values() and map.entries().
  • Iteration over Maps is always in insertion order.