The Wayback Machine - https://web.archive.org/web/20200704041721/https://www.geeksforgeeks.org/map-get-javascript/amp/

Map.get( ) In JavaScript

What is a map in JavaScript?

Differences between Objects and Maps in JavaScript
Both of these data structures are similar in many ways such as both store values using keys, allow retrieval of those values using keys, deletion of keys and verify whether a key holds any value or not. However, there are quite significant differences between Objects and Maps in JavaScript which make the usage of maps a better and more preferable option in many cases.

Map.get() Method in JavaScript
The Map.get() method in JavaScript is used for returning a specific element among all the elements which are present in a map.
The Map.get() method takes the key of the element to be returned as an argument and returns the element which is associated with the specified key passed as an argument. If the key passed as an argument is not present in the map, then Map.get() method returns undefined.
Applications:

  • Map.get() Method is used to get a specific element among all the elements which are present in a map.
  • Syntax:

    mapObj.get(key)

    Parameters Used:



    • key: It is the key of the element of the map which has to be returned.

    Return Value:

    • The Map.get() method returns the element which is associated with the specified key passed as an argument or undefined if the key passed as an argument is not present in the map.

    Examples of the above function are provided below.

    Examples:

    Input : var myMap = new Map();
            myMap.set(0, 'geeksforgeeks');
            document.write(myMap.get(0));
    
    Output: "geeksforgeeks"
    

    Explanation: In this example, a map object “myMap” has been created with a single [key, value] pair and the Map.get() method is used to return the element which is associated with the key ‘0’.

    Input : var myMap = new Map();
            myMap.set(0, 'geeksforgeeks');
            myMap.set(1, 'is an online portal');
            myMap.set(2, 'for geeks');
            document.write(myMap.get(0),"<br>");
            document.write(myMap.get(2),"<br>");
            document.write(myMap.get(4),"<br>");
    
    Output: "geeksforgeeks"
             "for geeks"
              undefined
    

    Explanation: In this example, a map object “myMap” has been created with a three [key, value] pairs and the Map.get() method is used to return the elements which are associated with the keys ‘0’, ‘2’ and ‘4’. Map.get() returns undefined when key passed is ‘4’ since the key ‘4’ is not present in the map.

    Code 1:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script>
        // creating a map object
        var myMap = new Map();
      
    // Adding [key, value] pair to the map
    myMap.set(0, 'geeksforgeeks');
      
    // displaying the element which is associated with
    // the key '0' using Map.get() method
    document.write(myMap.get(0));
      
    </script>
    chevron_right
    
    

    Output :

    "geeksforgeeks"
    

    Code 2:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script>
      
        // creating a map object
        var myMap = new Map();
      
    // Adding [key, value] pair to the map
    myMap.set(0, 'geeksforgeeks');
    myMap.set(1, 'is an online portal');
    myMap.set(2, 'for geeks');
      
    // displaying the elements which are associated with the keys '0', '2' 
    // and '4' using Map.get() method
    document.write(myMap.get(0),"</br>");
    document.write(myMap.get(2),"</br>");
    document.write(myMap.get(4),"</br>");
      
    </script>                    
    chevron_right
    
    

    Output :

    "geeksforgeeks"
    "for geeks"
    undefined
    

    Exceptions :





    Check out this Author's contributed articles.

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


    Article Tags :