Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Map.prototype.delete: remove an entry by key

The delete method removes the Map entry matching a supplied key and returns true only when such an entry existed.

Version: ECMAScript 2015 onward; example target: Node.js 24.22.2

Syntax

map.delete(key)

map.delete(key) looks up the key without type coercion. If an entry exists, it marks that record empty and returns true; otherwise it leaves the Map unchanged and returns false. The complete key–value pair is removed, and size decreases by one only after a successful deletion.

Keys use SameValueZero semantics: NaN matches NaN, and -0 matches +0; objects match only by reference identity. The order of remaining entries does not change. Calling set with the same key after deletion creates a new record at the end of iteration order, while active iterators skip the deleted record.

The receiver must be a real Map with Map internal data; using an object, Set, or unhandled Proxy as this throws TypeError. The method has been standard since ECMAScript 2015. Its boolean result distinguishes an absent key from an existing entry whose value was undefined.

Examples

const token = {};
const map = new Map([[token, "active"], [NaN, "unknown"], ["tail", 3]]);
console.log(map.delete({}), map.size);
console.log(map.delete(token), map.has(token), map.size);
console.log(map.delete(NaN), map.delete(NaN));

const ordered = new Map([["a", 1], ["b", 2]]);
ordered.delete("a");
ordered.set("a", 3);
console.log([...ordered.keys()].join(","));

try {
  Map.prototype.delete.call({}, "a");
} catch (error) {
  console.log(error instanceof TypeError);
}

Result

false 3
true false 2
true false
b,a
true

Sources