Map.prototype.has()

map.has(key)
Returns: boolean · Updated March 13, 2026 · Map and Set
map has contains

The has() method returns a boolean indicating whether a Map contains a specific key. Unlike object property checking with in operator, has() only checks the Map’s own entries and doesn’t traverse the prototype chain.

Syntax

map.has(key)

Parameters

ParameterTypeDescription
keyanyThe key to check for in the Map

Return Value

Returns true if the key exists in the Map, false otherwise.

Examples

Basic usage

const userRoles = new Map();
userRoles.set('alice', 'admin');
userRoles.set('bob', 'editor');

console.log(userRoles.has('alice'));
// true

console.log(userRoles.has('unknown'));
// false

Using with object keys

const visits = new Map();

const page1 = { path: '/home', lang: 'en' };
visits.set(page1, 1500);

console.log(visits.has(page1));
// true

// Different object reference - not found
console.log(visits.has({ path: '/home', lang: 'en' }));
// false

Conditional value retrieval

const cache = new Map();

function getCached(key) {
  if (!cache.has(key)) {
    return null;
  }
  return cache.get(key);
}

cache.set('users', ['alice', 'bob']);
console.log(getCached('users')); // ['alice', 'bob']
console.log(getCached('missing')); // null

Before delete operation

const queue = new Map([['job1', { status: 'running' }]]);

function cancelJob(jobId) {
  if (!queue.has(jobId)) {
    console.log(`Job ${jobId} not found`);
    return false;
  }
  
  queue.delete(jobId);
  console.log(`Job ${jobId} cancelled`);
  return true;
}

cancelJob('job1'); // Job job1 cancelled
cancelJob('job2'); // Job job2 not found

Common Patterns

Prevent overwriting existing entries

const registry = new Map();

function register(name, handler) {
  if (registry.has(name)) {
    throw new Error(`Handler ${name} already registered`);
  }
  registry.set(name, handler);
}

register('init', () => {});
register('init', () => {}); // Throws Error

Key existence before access

const configs = new Map([
  ['production', { debug: false }],
  ['development', { debug: true }]
]);

function getConfig(env) {
  const exists = configs.has(env);
  console.log(`Config for ${env} exists: ${exists}`);
  return configs.get(env);
}

getConfig('production'); // true
getConfig('staging'); // false - returns undefined

See Also