Example 1 — Read a Stored Value
Read an age that was stored for a name, and see what other lookups return.
const ages = new Map();
ages.set("Ann", 31);
ages.set("Bob", 27);
console.log(ages.get("Ann")); // 31
console.log(String(ages.get("Carl"))); // undefined
console.log(ages.size); // 2 How It Works
- A stored key gives its value.
ages.get("Ann")uses the same string that was passed toset(), so it finds 31. - A missing key gives undefined.
"Carl"was never stored, soget()answersundefined. It does not add a new entry. - Size stays the same. Reading never changes the Map. Size is still 2 after the lookups.

