November 8, 2023
size property returns the number of key/value pairs in the map. It provides an efficient way to determine the count without the need to iterate over the entire map.
It’s useful for things like:
- Validating the number of entries before performing operations.
- Checking map capacity against thresholds.
- Implementing logic that depends on the count of items in the map.
size property of a
Map. It's a straightforward way to get the count of entries, where each entry is a key-value pair.
How to use the size property
To access the
size property, you simply reference it on your
Map instance. Here's an example:
Size vs length
Unlike arrays, where you use
length to get the number of elements, for a
Map you must use
size. This is because
Map objects maintain the insertion order of elements and are a part of the ES6 specification.
How to Check if a Map is empty
You can check if a
Map is empty by comparing its
size property to
Limitations and considerations
size property is dynamic. If you add or remove items from the map, the
size will update accordingly. Keep in mind that
size is a property, not a method, so you do not call it with parentheses.
How to Truncate Date in MySQL