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

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




Reffered: https://www.geeksforgeeks.org


JavaScript

Related
Difference Between a Set and an Array Difference Between a Set and an Array
What is the use of the Add method in Sets in JavaScript ? What is the use of the Add method in Sets in JavaScript ?
How to Check if an Element Exists in a Set in JavaScript ? How to Check if an Element Exists in a Set in JavaScript ?
What Happens when you try to Add a Duplicate Value to a Set in JavaScript ? What Happens when you try to Add a Duplicate Value to a Set in JavaScript ?
How to Remove an Element from a Set in JavaScript ? How to Remove an Element from a Set in JavaScript ?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
12