The get method of the JavaScript Map object retrieves the value associated with a given key from that Map instance.
Syntax
The get function can be expressed using the syntax outlined below:
Example
mapObj.get(key)
Parameter
key - The key to be searched.
Return
The value of specified key.
JavaScript Map get method example
In this section, we will explore the get method by examining several examples.
Example 1
Let's see a simple example of get method.
Example
<script>
var map=new Map();
map.set(1,"jQuery");
map.set(2,"AngularJS");
map.set(3,"Bootstrap");
document.writeln(map.get(1)+"<br>");
document.writeln(map.get(2)+"<br>");
document.writeln(map.get(3));
</script>
Output:
Output
jQuery
AngularJS
Bootstrap
Example 2
Let’s examine a scenario to ascertain the outcome when a map object does not include the designated key element.
Example
<script>
var map=new Map();
map.set(1,"jQuery");
map.set(2,"AngularJS");
map.set(3,"Bootstrap");
document.writeln(map.get(5)); //returns undefined
</script>
Output:
Output
undefined