Map.prototype.keys()

map.keys()
Returns: Iterator · Updated March 13, 2026 · Map and Set
map keys iteration

The keys() method returns a new Iterator object containing all the keys in the Map. The keys are returned in the same order they were inserted into the Map.

Syntax

map.keys()

Parameters

None.

Return Value

A new Iterator object containing the keys of the Map.

Examples

Getting all keys as an array

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

console.log([...userRoles.keys()]);
// ['alice', 'bob', 'charlie']

Iterating with for…of

const settings = new Map([
  ['theme', 'dark'],
  ['language', 'en'],
  ['notifications', true]
]);

for (const key of settings.keys()) {
  console.log(key);
}
// theme
// language
// notifications

Check if Map has specific key

const cache = new Map([
  ['users', [Global_Objects::eval]],
  ['config', {}]
]);

const allKeys = [...cache.keys()];
console.log(allKeys.includes('users')); // true
console.log(allKeys.includes('cache')); // false

Use with Array methods

const products = new Map([
  ['apple', 1.5],
  ['banana', 0.75],
  ['orange', 2.0]
]);

const expensiveProducts = [...products.keys()].filter(key => products.get(key) > 1);
console.log(expensiveProducts);
// ['apple', 'orange']

Get keys with forEach

const sessions = new Map([
  ['session1', { user: 'alice' }],
  ['session2', { user: 'bob' }]
]);

sessions.forEach((value, key) => {
  console.log(`Session ${key}: ${value.user}`);
});

Common Patterns

Convert keys to unique set

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

const uniqueRoles = new Set([...roles.values()]);
console.log([...uniqueRoles]); // ['admin', 'editor', 'viewer']

Key existence check before operation

const permissions = new Map([
  ['read', true],
  ['write', false]
]);

function canPerform(action) {
  return permissions.get(action) ?? false;
}

console.log(canPerform('read')); // true
console.log(canPerform('delete')); // false

See Also