What is the Map.get method in JavaScript?
The get method of the Map object is used to get the value associated with the passed key.
Syntax
mapObj.get(key);
Arguments
key : The key for which the value is to be returned.
Return value
The get method returns the value associated with the passed key. If there is no entry for the passed key, then undefined is returned.
Code
let map = new Map();map.set("one", 1);map.set("two", 2);map.set("three", 3);console.log("Getting value of key one :", map.get("one") );console.log("Getting value of key three :", map.get("three") );console.log("Getting value of key four :", map.get("four") );
Explanation
In the code above:
- We have created a map object and added three entries to it.
one - 1
two - 2
three - 3
-
Then we called the
getmethod for the keyone,threeandfour. -
For the key
oneandthree, the value1and3is associated respectively. The result will be as follows:
map.get('one') will return 1
map.get('three') will return 3
- But, there is no value associated with the key
four. So,undefinedis returned forget('four').