Skip to content
Related Articles

Related Articles

JavaScript Map.size Property
  • Last Updated : 06 Apr, 2021

Map is a collection of elements where each element is stored as a Key, value pair. Map.size property is used return the number of Key, value pairs stored in a map.

Syntax:

mapName.size

Return Value: An integer representing number of Key, value pair stored in a map.

The examples below illustrate the Map.size property:

 



Example 1:

Javascript




<script>
  
    // Creating a new map
    let GFGmap = new Map();
  
    // Adding key, value pairs to the map
    GFGmap.set('1', 'Apple');
    GFGmapset('2', 'Banana');
    GFGmap.set('3', 'Mango');
  
    // Printing number of key, value
    // pairs stored in the map
    console.log(GFGmap.size);
  
</script>

Output:

3

Example 2:

Javascript




<script>
  
    // Creating a new map
    let GFGmap = new Map();
  
    // Adding key, value pairs to the map
    GFGmap.set('a', 'Apple');
    GFGmapset('b', 'Ball');
    GFGmap.set('c', 'Cat');
    GFGmap.set('d', 'Dog');
  
    // Printing number of key, value
    // pairs stored in the map
    console.log(GFGmap.size);
</script>

Output:

4
My Personal Notes arrow_drop_up
Recommended Articles
Page :