Open In App

How to Check the Size of a JavaScript Map ?

Last Updated : 01 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

To check the size (number of key-value pairs) of a Map in JavaScript, you can use the size property. The size property is a property of the Map object that returns the number of elements in the Map.

Example: Here, mapSize will be equal to 3 because there are three key-value pairs in the Map.

Javascript




let myMap = new Map();
 
myMap.set('key1', 'value1');
myMap.set('key2', 'value2');
myMap.set('key3', 'value3');
 
// Check the size of the Map
let mapSize = myMap.size;
 
console.log(mapSize); // Output: 3


Output

3

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads