Open In App

What is the use of the Get method in Maps in JavaScript ?

Last Updated : 01 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In JavaScript, the get() method is used with Map objects to retrieve the value associated with a specific key. The key is passed as an argument to the get() method, and the method returns the corresponding value if the key is found. If the key is not present in the Map, undefined is returned.

Syntax:

myMap.get(key);
  • myMap: The Map from which you want to retrieve a value.
  • key: The key for which you want to get the associated value.

Example: This example describes the Map() method to create the map object that contains the [key, value] pair to the map & displays the element that is associated with the specific key using the Map.get() method.

Javascript




// Creating a map object
let 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
console.log(myMap.get(0));


Output

GeeksforGeeks


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads