You are currently viewing Simple steps to Get Map Values in JavaScript using for of loop

Simple steps to Get Map Values in JavaScript using for of loop

Get Map Values JavaScript

In this article, we will learn about how to get map values in Javascript.

A map is an unordered collection of key-value pairs. Both keys and values can be of any data type. To retrieve a value, you can use the corresponding key. The map is iterable. They can be used with a for loop. A lot of these characteristics are similar to objects. So, let’s see the difference between a map and an object.

Object vs Map

  • Objects are unordered whereas maps are ordered.
  • Keys in objects can only be string or symbol type whereas in maps, they can be of any type.
  • An object has a prototype and may contain a few default keys which may collide with your own keys if you’re not careful. A map on the other hand does not contain any keys by default.
  • Objects are iterable whereas maps are iterables.
  • The number of items in an object must be determined manually whereas it is readily available with the size property in a map.
  • Apart from storing data, you can attach functionality to an object whereas maps are restricted to just storing data.


const map = new Map(['a', 1],['b',2])

for(const[key,value] of map){
   console.log('${key} : ${value}')

To create a map, we use the map constructor. The constructor optionally accepts an array as its argument. The array though should contain arrays of length 2 as elements. [a,1] & [b,2] here a and b are the keys 1 and 2 are the corresponding values in the two key-value pairs.
To log the values, we can use the for of loop to iterate over the map as in the above code.
If we run the code, we have got the output successfully. In the above output, a is the key 1 is the value and b is the key 2 is the value.

READ ALSO  Integer parseint() Function in JavaScript

If you to add a new value, we can use the below syntax:

map.set(key, value)


map.set('c', 3)

If you want to check, if a key exists in the map you can the “has method”.


If you want to find the length of the map, we can use the “size method”.


Leave a Reply