How to Find the Size of a Map in JavaScript

To find the size of a Map in JavaScript, you can use the “size” property and get the number of key-value pairs stored in the Map.

Example

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

In this code, we created a new Map called mainMap and used the “set()” method to add four key-value pairs.

In the next step, we used the “size” property to get the number of key-value pairs stored in the Map.

The output is 4, suggesting four key-value pairs in mainMap.

Leave a Comment