Map.prototype.get() - JavaScript | MDN
Try it
const map = new Map();
map.set("bar", "foo");
console.log(map.get("bar"));
// Expected output: "foo"
console.log(map.get("baz"));
// Expected output: undefined
Syntax
Parameters
Return value
The value associated with the specified key in the Map object. If the key can't be found, undefined is returned.
Examples
Using get()
js
const myMap = new Map();
myMap.set("bar", "foo");
console.log(myMap.get("bar")); // Returns "foo"
console.log(myMap.get("baz")); // Returns undefined
Using get() to retrieve a reference to an object
js
const arr = [];
const myMap = new Map();
myMap.set("bar", arr);
myMap.get("bar").push("foo");
console.log(arr); // ["foo"]
console.log(myMap.get("bar")); // ["foo"]
Specifications
| Specification |
|---|
| ECMAScript® 2027 Language Specification # sec-map.prototype.get |