Map.prototype.get()
Returns the value stored under a key, or undefined when the key is not present.
get(key)- Added in JavaScript ES2015
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | any | The key of the value to return. Object keys are compared by reference, not by value. |
Return value
anyThe value associated with the key, or undefined if the map has no entry for that key.
Errors
Throws a TypeError when called on a value that is not a real Map.
Changelog
- PHP ES2015Introduced with the Map built-in.
get() is the lookup half of a Map. It is a constant-time operation regardless of size, unlike scanning an array with find(), which is the usual reason people convert a list into a Map in the first place.
The undefined ambiguity
A missing key and a key deliberately stored with the value undefined both return undefined. If that distinction matters — caching a computation that can legitimately produce undefined, for example — use has() to test presence and get() only afterwards.
Supplying a default
There is no second "default value" argument. The idiomatic pattern is map.get(k) ?? fallback, which uses the fallback only for null and undefined, or map.get(k) || fallback if you also want it for 0 and "" — usually you do not.
Reference semantics
Objects come back as the same reference you stored, never a copy. Mutating the returned array or object is visible through the map, which is convenient (map.get(k).push(x)) and occasionally surprising when you expected isolation.
Keys match under SameValueZero, so NaN is retrievable and -0 finds an entry stored under 0.
Example
When to use it
- Replace a repeated array.find() lookup inside a loop
- Read a cached result and recompute only when it is missing
- Append to an array stored in the map without reassigning it
- Look up a per-object record keyed by the object itself
Frequently asked questions
Why does Map.get() return undefined?
Because the map has no entry for that key — or because the entry exists but its value is undefined. Call has() to tell the two apart.
How do I give Map.get() a default value?
There is no default parameter; write map.get(key) ?? fallback. Use ?? rather than || so that stored values of 0, "" or false are not replaced.
Why does get() not find my object key?
Map compares object keys by reference. An object literal written at lookup time is a different object from the one used as the key, even with identical contents.
More examples
Distinguishing a missing key from a stored undefined
Both get() calls give the same answer, but has() separates the two cases. This is the check a memoizing cache needs so it does not recompute a function that legitimately returned undefined.
const cache = new Map();
cache.set("computed", undefined);
console.log(cache.get("computed")); // undefined
console.log(cache.get("never-run")); // undefined
console.log(cache.has("computed")); // true
console.log(cache.has("never-run")); // falseGrouping into arrays with get-or-create
get() returns undefined on the first sighting of a city, so ?? [] creates the bucket. Because get() hands back the same array on later passes, push() is enough and the final set() is only needed for the first insertion.
const people = [
{ name: "ann", city: "Oslo" },
{ name: "bo", city: "Riga" },
{ name: "cy", city: "Oslo" },
];
const byCity = new Map();
for (const p of people) {
const bucket = byCity.get(p.city) ?? [];
bucket.push(p.name);
byCity.set(p.city, bucket);
}
console.log([...byCity]);
Discussion