The size property of the Map object in JavaScript returns the number of key/value pairs in the Map object.
The syntax of Map.size property is:
Map.prototype.size
This size property of Map returns the number of key/value pairs in the Map object.
Map.size returns value of type number
.
In JavaScript, we can use the size
property to get the number of key/value pairs in a Map object.
For example,
map
with initial key-value pairs.size
property to get the number of key/value pairs in the map
.console.log()
.const map = new Map([['a', 1], ['b', 2], ['c', 3]]);
const size = map.size;
console.log(size);
3
In JavaScript, we can use the size
property to get the number of key/value pairs in a Map object after adding an element.
For example,
map
with initial key-value pairs.set()
method to add a new key-value pair to the map
.size
property to get the updated number of key/value pairs in the map
.console.log()
.const map = new Map([['a', 1], ['b', 2]]);
map.set('c', 3);
const size = map.size;
console.log(size);
3
In JavaScript, we can use the size
property to get the number of key/value pairs in a Map object after removing an element.
For example,
map
with initial key-value pairs.delete()
method to remove a key-value pair from the map
.size
property to get the updated number of key/value pairs in the map
.console.log()
.const map = new Map([['a', 1], ['b', 2], ['c', 3]]);
map.delete('a');
const size = map.size;
console.log(size);
2
In this JavaScript tutorial, we learned about size property of Map: the syntax and few working examples with output and detailed explanation for each example.