How to iterate a Map in Javascript?
SrijanJan 29, 2025 - JavaScript
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.
Iterating map with for..of
We can use for..of to iterate a Map in Javascript.
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]
Iterating Map with forEach()
We can also iterate through a Map using forEach().
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.