JavaScript Map get() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Map

Definition

map.get(key) returns the value that was stored for key, or undefined when there is no such key. It never throws and never adds an entry. Keys are matched with SameValueZero, so NaN finds a NaN key. Use has() when you need to tell a missing key from a stored undefined.

Remember
map.get(key)  → the stored value, or undefined

key is stored       → its value
key not stored      → undefined
stored undefined    → looks like missing
different object    → undefined (identity)

ask has() when undefined can be a real value

Syntax

Call get on a Map with the key you used in set.

JavaScript
map.get(key)
CallWhat it does
map.get(key)Return the value stored for key, or undefined.
map.get(key) ?? fallbackUse ?? to supply a default when nothing is stored (or when the stored value is null).

Built into JavaScript since ES2015. get() only reads: it never adds a key. To check whether a key exists first, see has().

Library declarations
JavaScript
map.get(key)   // returns the value, or undefined

// key: any value; SameValueZero matching
// never throws

Parameters

ParameterDescription
keyRequired. The same value that was passed to set(). Object keys must be the same object by identity.

Return Value

Return ValueDescription
any valueThe value stored for the key.
undefinedNo entry for this key, or the stored value is undefined.

JavaScript Examples

Example 1 — Read a Stored Value

Read an age that was stored for a name, and see what other lookups return.

JavaScript
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
Try It Yourself

How It Works

  1. A stored key gives its value. ages.get("Ann") uses the same string that was passed to set(), so it finds 31.
  2. A missing key gives undefined. "Carl" was never stored, so get() answers undefined. It does not add a new entry.
  3. Size stays the same. Reading never changes the Map. Size is still 2 after the lookups.

Example 2 — Stored undefined or Missing Key?

Both look the same to get(). Ask has() to tell them apart.

JavaScript
const notes = new Map();

notes.set("home", undefined);

console.log(String(notes.get("home")));   // undefined
console.log(String(notes.get("about")));  // undefined
console.log(notes.has("home"));           // true
console.log(notes.has("about"));          // false
Try It Yourself

How It Works

  1. Undefined can be a real value. notes.set("home", undefined) is allowed. The entry exists even though its value is undefined.
  2. get() cannot tell the two apart. Both lookups print undefined, so the answer alone does not say whether the key is in the Map.
  3. has() settles it. has("home") is true and has("about") is false. Ask it first when a stored undefined is possible.

Example 3 — Supply a Default with ??

Read a score. When the key is missing, ?? gives a fallback of 0.

JavaScript
const scores = new Map();
scores.set("Ann", 10);

const ann = scores.get("Ann") ?? 0;
const bob = scores.get("Bob") ?? 0;

console.log(ann);   // 10
console.log(bob);   // 0
Try It Yourself

How It Works

  1. Read first. scores.get("Ann") finds 10. The ?? operator keeps that value because it is not nullish.
  2. Missing becomes the default. get("Bob") returns undefined, so ?? 0 supplies 0.
  3. Nothing was added. The Map still has only Ann. Using ?? does not call set().

Browser & Runtime Support

Map.prototype.get() is part of ES2015 (ES6), together with WeakMap, Set, and WeakSet.

✓ Baseline · ES2015

Map.prototype.get()

Works in every modern browser, plus Node.js, Deno, and Bun. Internet Explorer 11 has a basic Map with set(), get(), has(), delete(), clear(), and size.

98% Modern browser support
Google Chrome 36+ · Desktop & Mobile
Full support
Mozilla Firefox 6+ · Desktop & Mobile
Full support
Apple Safari 8+ · macOS & iOS
Full support
Microsoft Edge 12+
Full support
Node.js 0.12+ · every current release
Supported
Deno All releases
Supported
Internet Explorer IE 11 · no constructor argument
Basic
Map.prototype.get() Excellent

Bottom line: Safe to use in any modern browser and runtime. get() has behaved the same way since Map first shipped, and it never throws.

Frequently Asked Questions

It returns the value stored for the key, or undefined if the key is not in the Map. It never changes the Map and never adds a key.
No. A missing key simply returns undefined. get() never throws.
Call has(key). It returns true when the key is stored, even if the value is undefined, and false when it is missing. get() cannot tell the two apart.
Use the nullish coalescing operator: map.get(key) ?? fallback. That keeps real values like 0 or false, and only fills in when the result is null or undefined.
Map compares object keys by identity, not by content. Two objects with the same fields are two separate keys. Keep a reference to the exact object you used in set().

Next: has()

Check whether a key exists before you decide what the undefined answer means.

has() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful