To find the length of a Map in JavaScript, you can use the “size” property. The “size” property returns the number of key-value pairs stored in the Map.
Syntax
map.size
Example 1: How to Use Map.prototype.size
const mainMap = new Map();
mainMap.set('key1', 'value1');
mainMap.set('key2', 'value2');
mainMap.set('key3', 'value3');
mainMap.set('key4', 'value4');
console.log(mainMap.size);
Output
4
Example 2: Size of an empty map
let map1 = new Map();
let mapSize = map1.size;
console.log(mapSize);
Output
0
That’s it.

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.