Updated April 3, 2023
Introduction to JavaScript get Method
In Javascript, the Map.get() method is used to return a particular element among all the elements which are present in a map. This method gets the item key to be returned in the form of an argument and returns the item which is linked with the mentioned key passed as an argument. If the key which is passed in the form of an argument is not available on the map, then unreturned is returned by this method. Let us look into more details on get a method in JavaScript.
Syntax
Below is the syntax of the JavaScript get method.
mobj.get(key)
Here,
- mobj – the object of the map.
- key– The key that has to be searched.
Return value: Value of the mentioned key.
How does get Method works in JavaScript?
To know more about working, first, let us see what a map is. In Javascript, Map is a data structure that permits storing key-value [K, V] pairs where any value can be used as value or key depending on the requirement. In the map collection, the keys and values can be of any type, and if the value is inserted to the collection of the map using a key which by now exists in the collection, then the old value is replaced by the new value. Thus, the iteration of items in an object of the map is done in the order of insertion, and all key-value pairs are returned using the “for” loop for each iteration.
Now, let us move on to the working of the get method.
First, create an object for the map. Then, set the value using the set() method. Finally, the values in the map can be retrieved using the get() method. If there is no element, then undefined will be returned. The following is a sample code for the same.
<!DOCTYPE html>
<html>
<body>
<script>
var mapobj = new Map();
mapobj.set(0, 'Happy moments');
document.write(mapobj.get(0));
</script>
</body>
</html>
Examples of JavaScript get Method
Below are some of the sample programs using the get() method.
Example #1
Java script program to add an element in the map and retrieve it using the get() method.
Code:
<!DOCTYPE html>
<html>
<body>
<script>
var mapobj = new Map();
mapobj.set(0, 'Happy moments');
document.write(mapobj.get(0));
</script>
</body>
</html>
Output:
In this program, first, create an object for the map. Here, the object is mapobj. Then, set the value “Happy moments” using the set() method. The values in the map are then retrieved using the get() method. On executing the code, Happy moments will be displayed.
Example #2
Java script program to add multiple elements in the map and retrieve them using the get() method.
Code:
<!DOCTYPE html>
<html>
<body>
<script>
var mapobj = new Map();
mapobj.set(0, 'Happy moments');
mapobj.set(1, 'Hai Jinny');
mapobj.set(2, 'Hai Anna');
mapobj.set(3, 'Hai Anjana');
mapobj.set(4, 'Hai Kim');
mapobj.set(4, 'Hai Kin');
document.write(mapobj.get(0) +"<br>" );
document.write(mapobj.get(1) +"<br>" );
document.write(mapobj.get(2) +"<br>" );
document.write(mapobj.get(3) +"<br>" );
document.write(mapobj.get(4) +"<br>" );
</script>
</body>
</html>
Output:
In this program, also, first, create an object for the map. Here, the object is mapobj. Then, set the values using the set() method and the values in the map are then retrieved using the get() method. Finally, on executing the code, all the values will be displayed.
Example #3
Java script program to add multiple elements in the map and retrieve it using the get() method with an extra get() method.
Code:
<!DOCTYPE html>
<html>
<body>
<script>
var mapobj = new Map();
mapobj.set(0, 'Happy moments');
mapobj.set(1, 'Hai Jinny');
mapobj.set(2, 'Hai Anna');
mapobj.set(3, 'Hai Anjana');
mapobj.set(4, 'Hai Kim');
mapobj.set(4, 'Hai Kin');
document.write(mapobj.get(0) +"<br>" );
document.write(mapobj.get(1) +"<br>" );
document.write(mapobj.get(2) +"<br>" );
document.write(mapobj.get(3) +"<br>" );
document.write(mapobj.get(4) +"<br>" );
document.write(mapobj.get(5) +"<br>" );
</script>
</body>
</html>
Output:
Here also, create an object for the map. Then, set the value using the set() method. Unlike the above programs, use an extra get() method. It is to check what will be the output if no element is present. On executing the code, it can be seen that 5 values along with an undefined one are displayed. That is, if there is no element, then undefined will be returned.
Example #4
Java script program to add multiple elements in the map and retrieve elements and size details.
Code:
<!DOCTYPE html>
<html>
<body>
<script>
var mapobj = new Map();
mapobj.set('2', 'strval');
mapobj.set(2, 'numval');
mapobj.set(true, 'boolval');
alert( mapobj.get(2) );
alert( mapobj.get('2') );
alert( mapobj.size );
</script>
</body>
</html>
Output:
An object for the map is created first.Then, set the values using the set() method. Here, a string value, Boolean value, and a number value are set for the same value 2. On executing the code, it can be seen that a popup is displayed, as shown in the sample output. That is, on executing the first get statement, numval gets displayed.
The string value then displays second, as shown below.
Then, the size of the map is also retrieved at last.
Example #5
Java script program to add multiple elements in the map and retrieve elements and size details.
Code:
<!DOCTYPE html>
<html>
<body>
<script>
var nameobj = { name: "Anna" };
var countobj = new Map();
countobj.set(nameobj, 166);
alert( countobj.get(nameobj) );
</script>
</body>
</html>
Output:
In this program, two objects are created for name and countThen, set the value using a set() method and the value in the map is then retrieved using the get() method. On executing the code, the value will be displayed.
Example #6
Java script program to add multiple elements in the map using chaining and retrieving it.
Code:
<!DOCTYPE html>
<html>
<body>
<script>
var mapobj = new Map();
mapobj.set('1', 'strval')
.set(1, 'numval')
.set(true, 'boolval');
alert( mapobj.get(1) );
alert( mapobj.get('1') );
</script>
</body>
</html>
Output:
An object for the map is created first. Then, using the chaining method, values are set. Finally, on executing the code, it can be seen that a popup is displayed, as shown in the sample output. That is, on executing the first get statement, numval gets displayed.
The string value then displays second, as shown below.
Conclusion
Map.get() method is used to return a particular element among all the elements which are present in a map. In this article, different aspects such as syntax, working, and examples of the get() method are explained in detail.
Recommended Articles
We hope that this EDUCBA information on “JavaScript get Method” was beneficial to you. You can view EDUCBA’s recommended articles for more information.