Map.prototype.delete()

Removes the entry for a key and returns whether an entry was actually removed.

Signature
delete(key)
  • Added in JavaScript ES2015

Parameters

ParameterTypeDescription
$keyanyThe key of the entry to remove from the Map. Object keys are compared by reference, not by value.

Return value

booleantrue if an entry existed for the key and has been removed, false if the key was not found.

Errors

Throws a TypeError when called on a value that is not a real Map.

Changelog

  • PHP ES2015Introduced with the Map built-in.

delete() removes a single entry and tells you whether there was one. That boolean is genuinely useful: it collapses the common if (map.has(k)) { map.delete(k); ... } into if (map.delete(k)) { ... } with one hash lookup instead of two.

Deleting during iteration

Unlike an array, a Map is safe to delete from while you iterate it. Entries removed before the iterator reaches them are simply skipped; entries already visited are unaffected. This makes "iterate and prune" loops straightforward with forEach() or for...of, with no index bookkeeping and no need to collect keys first.

Order after deletion

Deleting does not shift anything. If you later re-add the same key with set() it goes to the end of the iteration order, because it is a new insertion. That delete-then-set pair is in fact the standard trick for implementing an LRU cache, where a plain overwrite would not move the entry.

The method name is a reserved word, but it is a normal method here — map.delete(k), not the delete operator.

Example

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

When to use it

  • Evict a stale entry from a cache and log only if it existed
  • Prune entries in place while iterating the map
  • Move a key to the end of insertion order for an LRU policy
  • Release an object key so the value can be garbage collected

Frequently asked questions

What does Map.delete() return?

It returns true if an entry for that key existed and was removed, and false if the key was not in the map.

Can I delete from a Map while iterating over it?

Yes. Entries deleted before the iterator reaches them are skipped, and already-visited entries are unaffected, so prune-in-place loops are safe.

How do I move a Map key to the end of the order?

Delete it and set it again. Re-inserting counts as a new insertion, while a plain overwrite keeps the entry in its original position.

More examples

Pruning while iterating

Removing entries mid-loop is well defined for a Map: the iterator simply never yields a deleted entry it has not reached yet. The same pattern on an array with splice() would skip elements.

Example · javascript
const scores = new Map([
  ["ann", 90], ["bo", 40], ["cy", 70], ["di", 20],
]);

for (const [name, score] of scores) {
  if (score < 50) scores.delete(name);
}

console.log([...scores]);
console.log(scores.size);

Delete then set to move a key last

The first set() leaves "a" at the front because overwriting never reorders. After delete-then-set it becomes the most recent entry, so keys().next() reliably names the eviction candidate.

Example · javascript
const lru = new Map([["a", 1], ["b", 2], ["c", 3]]);

lru.set("a", 1);                 // overwrite: position unchanged
console.log([...lru.keys()]);    // a, b, c

lru.delete("a");
lru.set("a", 1);                 // re-insert: goes last
console.log([...lru.keys()]);    // b, c, a
console.log(lru.keys().next().value); // oldest key

Discussion

  • Be the first to comment on this lesson.