How to get the Length of a Map in JavaScript


Borislav Hadzhiev

Last updated: Oct 19, 2021


Check out my new book

Get the Length of a Map in JavaScript #

Use the size property to get the length of a Map - map.size. The size property returns the number of elements in the Map object. When accessed on an empty Map, the size property returns 0.

const map = new Map(); map.set('name', 'Tom'); map.set('age', 30); map.set('country', 'Chile'); console.log(map.size); // 👉️ 3

We used the Map.size property to get the number of key-value pairs the Map stores.

The property is very similar to an array's length property and returns an integer representing how many items the Map contains.

As opposed to the array's lenth property, the size property is read-only and can't be changed by the user.

const map = new Map(); map.set('name', 'Tom'); console.log(map.size); // 👉️ 1 map.size = 5; console.log(map.size); // 👉️ 1

Even though we tried to set the size of the Map, we were unable to. This is not the case when using the array's length property.

const arr = ['a', 'b', 'c']; console.log(arr.length); // 👉️ 3 arr.length = 1; console.log(arr.length); // 👉️ 1

Further Reading #

Use the search field on my Home Page to filter through my more than 3,000 articles.