Map.prototype.delete
Removes the specified element from a Map object by key
Syntax
map.delete(key)Parameters
| Parameter | Type | Description |
|---|---|---|
| key | K | The key of the element to remove |
Return Value
true if an element existed and has been removed, false otherwise
Examples
const map = new Map([['a', 1], ['b', 2]]);
console.log(map.delete('a')); // true
console.log(map.delete('c')); // false
console.log(map.size); // 1const sessions = new Map<string, { user: string }>();
sessions.set('abc123', { user: 'Alice' });
function logout(token: string) {
return sessions.delete(token);
}const map = new Map([['x', 1], ['y', 2], ['z', 3]]);
for (const [key, val] of map) {
if (val < 2) map.delete(key);
}
console.log([...map.keys()]); // ['y', 'z']Understanding Map.prototype.delete
The Map.prototype.delete method in JavaScript removes the specified element from a Map object by key. It belongs to the Map object and is one of the most widely used methods for working with map values in modern JavaScript and TypeScript applications.
The method signature is map.delete(key). It accepts 1 parameter: key. When called, it returns true if an element existed and has been removed, false otherwise. Understanding when and how to use delete() helps you write more expressive, readable code.
Common use cases for Map.prototype.delete include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like map-clear, map-has, map-set, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for Map.prototype.delete is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
Map.prototype.clearRemoves all key-value pairs from the Map object
Map.prototype.hasReturns a boolean indicating whether a value associated with the specified key exists in the Map or not
Map.prototype.setAdds or updates an entry in a Map object with a specified key and value
Set.prototype.deleteRemoves the specified element from a Set object
More Map Methods
Other methods in the Map object
Related Tools
More Map Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.