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()
Call
What 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
Parameter
Description
(none)
This method takes no parameters. Call it as map.keys().
Return Value
Return Value
Description
Map Iterator
An 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);
}
Two keys go in first. Ann is stored, then Bob. That sets the insertion order.
Updating does not move Ann.set("Ann", 12) replaces the value. Ann stays the first key.
Order and size hold.keys() still lists Ann before Bob, the new score is 12, and size is still 2.
Compatibility
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 Chrome36+ · Desktop & Mobile
Full support
Mozilla Firefox6+ · Desktop & Mobile
Full support
Apple Safari8+ · macOS & iOS
Full support
Microsoft Edge12+
Full support
Node.js0.12+ · every current release
Supported
DenoAll releases
Supported
Internet ExplorerIE 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.