In JavaScript, we can easily find the size of a map using the map size property on a map object.
map.size;
In JavaScript, the map object is useful for holding key-value pairs.
When working with objects in JavaScript, the ability to get the size or length of an object can be useful for use in loops.
To get the length of a map in JavaScript, we can use the map size property. The JavaScript map size property is read only and returns an integer value.
Let’s say we have the following JavaScript map.
var map = new Map();
map.set('Apple','Good');
map.set('Pear','No');
map.set('Banana','Maybe');
map.set('Lemon','Yes');
To get the size of the map, we use the JavaScript map size property.
console.log(map.size);
// Output:
4
Something to note here is that the map object does not have a ‘length’ property. If you try to access the ‘length’ property of a map object, you will get ‘undefined’.
console.log(map.length);
// Output:
undefined
Hopefully this article has been useful for you to understand how to get the size of a JavaScript Map.
Leave a Reply