Map.prototype.get()

Straightforward reading, with one gap: undefined means both "not there" and "there, holding undefined". has() is how you tell them apart.

Map methodES2015Live demo
Common call
map.get(key)
Returns
the value, or undefined
Replaces
obj[key], for non-string keys
Watch out
undefined is ambiguous — use has() if it matters
map.get(keykey — The key to look up, matched by SameValueZero. No coercion happens — 1 and "1" are different keys.type: any · required)
→ any

Demo

Live evaluation
Try:
Inputs
jsonstringJSON pairs, e.g. [["a",1]]
kstringkey to look up
Output
new Map(JSON.parse('[["a",1],["b",2]]')).get('a')
1

Present keys return their value; absent keys return undefined. The third case shows null is returned as null — only a genuinely missing key gives undefined. The fourth is the one to watch: the Map was built with the NUMBER 1 as its key, and looking up the STRING '1' misses entirely. A plain object would have found it, because object keys are always strings. Map does no coercion, which is a feature and a trap in equal measure.

Parameters

NameTypeRequiredDescription
keyanyyesThe key to look up, matched by SameValueZero. No coercion happens — 1 and "1" are different keys.

Return value

any — The stored value, or undefined when the key is absent. Those two cases are indistinguishable if you ever store undefined.

Common patterns

Default for a missing key
Nullish coalescing handles the undefined.
const count = map.get(key) ?? 0;
Get-or-create
The standard accumulator idiom.
if (!map.has(key)) map.set(key, []);
map.get(key).push(item);
Distinguish absent from undefined
Only has() can tell you.
const present = map.has(key);
const value = map.get(key);

Examples

1. Present
new Map([["a", 1]]).get("a")
Returns
1
2. Absent
new Map().get("a")
Returns
undefined
3. Stored undefined
new Map([["a", undefined]]).get("a")
Returns
undefined
4. has disambiguates
new Map([["a", undefined]]).has("a")
Returns
true
5. No coercion
new Map([[1, "x"]]).get("1")
Returns
undefined
6. Objects by reference
const k = {}; new Map([[k, 1]]).get(k)
Returns
1

Pitfalls

1. undefined is ambiguous
A missing key and a key holding undefined both return undefined. If your values can legitimately be undefined — a cache of results where "computed, but no answer" is meaningful — get alone cannot tell you whether work has been done.
Indistinguishable
new Map([["a", undefined]]).get("a")
undefined
Ask has()
new Map([["a", undefined]]).has("a")
true
2. No key coercion, unlike an object
obj[1] and obj["1"] are the same property; map.get(1) and map.get("1") are not. Data whose keys arrive as strings from JSON will not match a Map built with numbers, and nothing warns you.
Misses
new Map([[1, "x"]]).get("1")
undefined
Normalise the key
new Map([[1, "x"]]).get(Number("1"))
'x'
3. Double lookup in the get-or-create pattern
has-then-get-then-set walks the hash three times. Fine for most code; in a hot loop, get once and check the result instead.
Three lookups
if (!m.has(k)) m.set(k, []);
m.get(k).push(x);
works
Two
let a = m.get(k);
if (!a) m.set(k, a = []);
a.push(x);
same result
4. It is a method, not bracket access
map["a"] does not read an entry — it looks for a property named "a" on the Map object itself, which does not exist. The entry is invisible to bracket syntax, and to spread and JSON.
Property access
const m = new Map([["a", 1]]);
m["a"]
undefined
Use get
m.get("a")
1

When to use

Use it
  • Reading a value by a key of any type
  • Lookups where keys must not be coerced to strings
  • Caches and indexes keyed by objects or numbers
Reach for something else
  • You only need to know whether the key exists → has
  • Values may be undefined and that matters → pair with has
  • The data is a fixed record → an object, with property access
  • You want a default without a check → Object.groupBy, or ?? at the call site

Notes

Complexity
O(1) average
Return
The stored value by reference; nothing is copied
CPython impl
V8: Builtins-map-get
Memory
No allocation
Thread-safe
Single-threaded; the Map is only read

FAQ

Nullish coalescing on the result. Use ?? rather than || so that a legitimately stored 0, empty string or false is not replaced by the default.

const n = map.get(key) ?? 0;   // keeps a stored 0
const bad = map.get(key) || 0; // replaces it

History

ES2015
Map added with get, set, has, delete and the iteration methods.