Map.prototype.get()

map.get(key)
Returns: any | undefined · Added in vES6 · Updated March 13, 2026 · Map and Set
map get retrieve es6 collection key-value

The get() method retrieves the value associated with a specified key from a Map object. If the key doesn’t exist, it returns undefined. This behavior differs subtly but importantly from plain object property access.

Syntax

map.get(key)

Parameters

ParameterTypeDefaultDescription
keyanyThe key whose value to retrieve

Return Value

The value associated with the key, or undefined if the key doesn’t exist. Unlike objects, undefined is a valid Map value—you can store undefined as a value and distinguish it from a missing key only by using has().

Examples

Basic Retrieval

const userRoles = new Map([
  ['alice', 'admin'],
  ['bob', 'editor'],
  ['charlie', 'viewer']
]);

console.log(userRoles.get('alice'));   // "admin"
console.log(userRoles.get('bob'));     // "editor"
console.log(userRoles.get('charlie')); // "viewer"

// Key doesn't exist - returns undefined
console.log(userRoles.get('dave'));    // undefined

Handling Missing Keys Gracefully

const config = new Map([
  ['theme', 'dark'],
  ['language', 'en']
]);

// Safe retrieval with fallback
function getConfig(key, defaultValue) {
  return config.get(key) ?? defaultValue;
}

console.log(getConfig('theme', 'light'));    // "dark" (exists)
console.log(getConfig('fontSize', '16px'));  // "16px" (fallback)

// Direct access without fallback - careful!
const timeout = config.get('timeout');
if (timeout === undefined) {
  console.log('No timeout set');  // This runs for missing keys
}

Maps with Object Keys: Where get() Excels

This is where Maps truly shine—using objects as keys where plain objects fail:

const widgetRegistry = new Map();

const buttonWidget = { type: 'button', id: 'btn-1' };
const inputWidget = { type: 'input', id: 'inp-1' };

// Store metadata for each widget object
widgetRegistry.set(buttonWidget, { clicks: 150, shown: true });
widgetRegistry.set(inputWidget, { focusCount: 42, shown: true });

// Retrieve using the same object reference
console.log(widgetRegistry.get(buttonWidget)); 
// { clicks: 150, shown: true }

// Plain objects can't do this:
const plainObj = {};
const key = { a: 1 };
plainObj[key] = 'value';
console.log(plainObj[{ a: 1 }]); // undefined! Different object reference

The has() + get() Pattern

For non-trivial logic, always check with has() before retrieving:

const cache = new Map();

function getUserData(userId) {
  // Check existence first - critical for falsy but valid values!
  if (!cache.has(userId)) {
    // Simulate fetching from database
    const freshData = fetchFromDB(userId);
    cache.set(userId, freshData);
  }
  
  // Now safe to get
  return cache.get(userId);
}

// Why has() matters: distinguishing undefined value from missing key
const settings = new Map();
settings.set('debug', undefined);

console.log(settings.get('debug')); // undefined
console.log(settings.has('debug')); // true - key exists!

console.log(settings.get('missing')); // undefined  
console.log(settings.has('missing')); // false - key doesn't exist

Map.get() vs Object Property Access

Aspectmap.get(key)obj[key] or obj.key
Key typesAny value (objects, functions, primitives)Strings/symbols only
Missing keyReturns undefinedReturns undefined
Prototype pollutionImmuneInherits from prototype
OrderMaintains insertion orderNot guaranteed
Sizemap.size (O(1))Object.keys(obj).length (O(n))

Performance Note

See Also