How to Get the Length of a Map in JavaScript

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.