Map.prototype.values()

Returns an iterator over the map's values in insertion order.

Signature
values()
  • Added in JavaScript ES2015

Return value

Map IteratorA new iterable iterator object yielding each value in the map, 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.
  • PHP ES2025Iterator helper methods such as map(), filter() and reduce() became available on the returned iterator via Iterator.prototype.

values() yields the stored values, in the order their keys were first inserted, with no way back to the key. When you find yourself calling get() inside a loop over keys(), this is what you wanted.

Duplicates are kept

Unlike keys, values are not deduplicated. Three entries holding the same object yield that object three times, and [...map.values()].length always equals map.size. If you want the distinct values, wrap the result: new Set(map.values()).

Aggregating

The return value is an iterator, so array methods are not directly available in older engines. Spread it to reduce or sum: [...map.values()].reduce(...). Modern engines also expose iterator helpers, letting you write map.values().filter(v => v > 0).toArray() without materializing an intermediate array — useful for large maps.

Objects come back as the same references that were stored, so mutating one changes what the map holds. The iterator is live and single-use, exactly like keys(): consume it once, and create it fresh each time you need it.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Sum or average every number stored in the map
  • Collect the distinct values with new Set(map.values())
  • Feed all stored records into an array method without the keys
  • Count how many entries satisfy a condition on their value

Frequently asked questions

How do I get all the values of a Map as an array?

Use [...map.values()] or Array.from(map.values()). The method itself returns an iterator, not an array.

Does Map.values() remove duplicate values?

No. It yields one value per entry, so the same value appears once for each key holding it. Wrap it in new Set(map.values()) to deduplicate.

How do I sum the values in a Map?

Spread and reduce: [...map.values()].reduce((a, b) => a + b, 0). In engines with iterator helpers you can also call map.values().reduce(...) directly.

More examples

Values repeat; keys do not

The iterator produces exactly map.size values, duplicates included. Passing it straight to a Set gives the distinct roles, still in first-seen order.

Example · javascript
const roles = new Map([
  ["ann", "admin"],
  ["bo", "user"],
  ["cy", "admin"],
]);

console.log([...roles.values()]);          // admin, user, admin
console.log(roles.size);                   // 3
console.log([...new Set(roles.values())]); // admin, user

Values are shared references

values() hands back the stored references, not copies, so incrementing through the loop mutates the one object both entries point at. Clone inside the loop if you need isolated values.

Example · javascript
const shared = { hits: 0 };
const m = new Map([["a", shared], ["b", shared]]);

for (const v of m.values()) v.hits++;

console.log(shared.hits);        // 2 — same object twice
console.log(m.get("a") === m.get("b")); // true
console.log(m.get("a"));

Discussion

  • Be the first to comment on this lesson.