Map.prototype.entries()
Returns an iterator over the map's [key, value] pairs in insertion order.
entries()- Added in JavaScript ES2015
Return value
Map IteratorA new iterable iterator object yielding a two-element [key, value] array for each entry, in insertion order.
Errors
Throws a TypeError when called on a value that is not a real Map.
Changelog
- PHP ES2015Introduced with the Map built-in; Map.prototype[Symbol.iterator] is the same function.
- PHP ES2019Object.fromEntries() added, making entries() a direct route back to a plain object.
- PHP ES2025Iterator helper methods became available on the returned iterator via Iterator.prototype.
entries() yields [key, value] pairs, which destructure cleanly in a loop: for (const [k, v] of map.entries()).
You rarely need to write it
Map.prototype[Symbol.iterator] is Map.prototype.entries — the same function object. So for (const [k, v] of map), [...map] and new Map(otherMap) all go through this method already. Writing .entries() explicitly is a readability choice, and it is the natural symmetry when the surrounding code also uses keys() or values().
Round trips
Because the constructor accepts an iterable of pairs, new Map(map.entries()) makes a shallow copy. Object.fromEntries(map) converts to a plain object — remember that every key is stringified on the way, so numeric and object keys do not survive. Going the other way, new Map(Object.entries(obj)) lifts an object into a Map.
Each pair is a freshly allocated array, so mutating it does nothing to the map — pair[1] = x is silently useless; call set() instead. Like the other iterators it is live and single-use.
Example
When to use it
- Loop over keys and values together with destructuring
- Convert a map to a plain object for JSON serialization
- Sort entries by value and rebuild a map in the new order
- Copy a map with new Map(map.entries())
Frequently asked questions
What is the difference between map.entries() and iterating the map directly?
There is none. Map.prototype[Symbol.iterator] is the same function as entries(), so for...of over the map yields [key, value] pairs already.
How do I convert a Map to a plain object?
Use Object.fromEntries(map). Every key is coerced to a string, so numeric, object and symbol keys will not round-trip.
Can I change a value by assigning to the destructured pair?
No. Each pair is a fresh array, so pair[1] = x is discarded. Call map.set(key, x) instead.
More examples
Sorting a map by value
A Map cannot be sorted in place, so the entries are spread into an array, ordered by the value at index 1, and fed straight back to the constructor. The rebuilt map iterates highest-first.
const scores = new Map([["ann", 72], ["bo", 95], ["cy", 84]]);
const ranked = new Map(
[...scores.entries()].sort((a, b) => b[1] - a[1]),
);
console.log([...ranked.keys()]);
console.log(ranked.entries().next().value);
console.log(ranked.size === scores.size);Pairs are copies, not views
Each iteration allocates a new two-element array, so assigning into it is discarded. Only set() changes the map, and calling it on an existing key during iteration is safe.
const m = new Map([["a", 1]]);
for (const pair of m.entries()) {
pair[1] = 99; // writes to the temporary array
console.log(pair);
}
console.log(m.get("a")); // 1 — unchanged
for (const [k] of m) m.set(k, 99);
console.log(m.get("a")); // 99
Discussion