Map.prototype.clear
Removes all key-value pairs from the Map object
Syntax
map.clear()Return Value
undefined
Examples
const map = new Map([['a', 1], ['b', 2], ['c', 3]]);
console.log(map.size); // 3
map.clear();
console.log(map.size); // 0const cache = new Map<string, unknown>();
function resetCache() {
cache.clear();
console.log('Cache cleared');
}const map = new Map([['x', 1]]);
map.clear();
console.log(map.has('x')); // falseUnderstanding Map.prototype.clear
The Map.prototype.clear method in JavaScript removes all key-value pairs from the Map object. 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.clear(). When called, it returns undefined. Understanding when and how to use clear() helps you write more expressive, readable code.
Common use cases for Map.prototype.clear include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like map-delete, map-set, set-clear, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for Map.prototype.clear 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
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.