JavaScript Map keys() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Map

Definition

map.keys() returns an iterator that gives you every key of the Map, in the order the keys were first added. Changing a value with set() does not move the key. Use for...of, Array.from(), or the spread operator to work with the iterator.

Remember
map.keys()  → iterator of keys

order           → insertion order
for...of        → loop each key
Array.from(...) → make an array
[...map.keys()] → same as Array.from

changing a value does not move the key

Syntax

Call it on a Map. It takes no arguments and is usually used with for...of.

JavaScript
map.keys()
CallWhat it does
map.keys()An iterator of the keys of the Map, in insertion order.
[...map.keys()]Spread the iterator into an array.
Array.from(map.keys())Turn the iterator into an array another way.

Built into JavaScript since ES2015. A Map is also iterable on its own: for (const [key, value] of map) walks entries. To store the keys you loop over, see set().

Library declarations
JavaScript
map.keys()   // returns a Map Iterator of keys

// no parameters
// insertion order; live view of current keys

Parameters

ParameterDescription
(none)This method takes no parameters. Call it as map.keys().

Return Value

Return ValueDescription
Map IteratorAn iterator of keys. Use it once with for...of, or turn it into an array.

JavaScript Examples

Example 1 — Loop Over Keys with for...of

Store three ages, then print every key in the order it was added.

JavaScript
const ages = new Map();
ages.set("Ann", 31);
ages.set("Bob", 27);
ages.set("Carl", 40);

for (const name of ages.keys()) {
  console.log(name);
}
Try It Yourself

How It Works

  1. Fill in order. Ann, Bob, and Carl are stored one after another with set().
  2. keys() gives an iterator. ages.keys() returns an iterator of those names, not the ages.
  3. for...of walks insertion order. The loop prints Ann, then Bob, then Carl, matching the order of the first set() for each key.

Example 2 — Turn Keys into an Array

Collect product ids with Array.from() and with the spread operator.

JavaScript
const stock = new Map([
  ["mug", 12],
  ["cup", 4],
  ["bowl", 7]
]);

const fromArray = Array.from(stock.keys());
const fromSpread = [...stock.keys()];

console.log(fromArray.join(", "));    // mug, cup, bowl
console.log(fromSpread.join(", "));   // mug, cup, bowl
Try It Yourself

How It Works

  1. Start from pairs. The Map is built from three product ids in a fixed order.
  2. Array.from reads the iterator. Array.from(stock.keys()) walks every key and builds a normal array.
  3. Spread does the same job. [...stock.keys()] also expands the iterator into an array with the same order.

Example 3 — Keys Stay After a Value Change

Replace a score for an existing name. The key order does not change.

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

scores.set("Ann", 12);

console.log(Array.from(scores.keys()).join(", "));  // Ann, Bob
console.log(scores.get("Ann"));                     // 12
console.log(scores.size);                           // 2
Try It Yourself

How It Works

  1. Two keys go in first. Ann is stored, then Bob. That sets the insertion order.
  2. Updating does not move Ann. set("Ann", 12) replaces the value. Ann stays the first key.
  3. Order and size hold. keys() still lists Ann before Bob, the new score is 12, and size is still 2.

Browser & Runtime Support

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

✓ Baseline · ES2015

Map.prototype.keys()

Works in every modern browser, plus Node.js, Deno, and Bun. Older Internet Explorer builds may lack Map iterators; modern browsers support keys() fully.

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.keys() Excellent

Bottom line: Safe to use in any modern browser and runtime. keys() returns a live iterator of keys in insertion order.

Frequently Asked Questions

It returns an iterator that gives every key of the Map, in the order the keys were first added. It does not return an array unless you build one.
Use for (const key of map.keys()). You can also use Array.from(map.keys()) or [...map.keys()] when you need an array.
No. Calling set() again with an existing key updates the value but leaves the key in its original insertion place.
Not quite. for (const [key, value] of map) walks entries. map.keys() walks only keys. Both follow insertion order.
Each call to keys() creates a new iterator. You can call keys() again anytime. A single iterator object is consumed as you walk it.

Next: values()

Store keys with set(), then read them back with get().

values() 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