JavaScript – Get Size of Map
To get the size of a Map in JavaScript, access size property of this Map. size is a read only property of Map object and returns an integer value representing the number of key-value pairs in this Map.
Syntax
The syntax to access size property on the Map map
is
</>
Copy
map.size;
Examples
In the following example, we take a Map with two items and find the size of this Map programmatically using size property.
index.html
<!DOCTYPE html>
<html lang="en">
<body>
<pre id="output"></pre>
<script>
var map1 = new Map([
['a', 10],
['b', 20]
]);
var mapSize = map1.size;
var displayOutput = 'Size of map is ' + mapSize + '.';
document.getElementById('output').innerHTML += displayOutput;
</script>
</body>
</html>
The size of an empty map is zero.
index.html
<!DOCTYPE html>
<html lang="en">
<body>
<pre id="output"></pre>
<script>
var map1 = new Map();
var mapSize = map1.size;
var displayOutput = 'Size of map is ' + mapSize + '.';
document.getElementById('output').innerHTML += displayOutput;
</script>
</body>
</html>
Conclusion
In this JavaScript Tutorial, we learned how to get the size of a Map in JavaScript using size property, with examples.