Last Updated : 11 Jul, 2025
JavaScript Map.clear() method is used for the removal of all the elements from a map and making it empty. It removes all the [key, value] from the map. No arguments are required to be sent as parameters to the Map.clear() method and it returns an undefined return value.
Syntax:mapObj.clear()Parameters:
Examples of the above method are provided below.
Example 1: In this example, a map object "myMap" has been created with a single [key, value] pair and the Map.clear() method is used to remove the [key, value] pair from "myMap". myMap.size() is used to check the number of [key, value] pairs belonging to the map object.
javascript
// creating a map object
let myMap = new Map();
// Adding [key, value] pair to the map
myMap.set(0, 'geeksforgeeks');
// displaying the number of
// [key, value] pairs the map has
console.log(myMap.size);
// removing the [key, value] pairs of
// the map using Map.clear() method
myMap.clear();
// displaying the number of
// [key, value] pairs the map has
console.log(myMap.size);
Output:
1
0
Example 2: In this example, a map object "myMap" has been created with a three [key, value] pairs and the Map.clear() method is used to remove all the [key, value] pairs from "myMap". myMap.size() is used to check the number of [key, value] pairs belonging to the map object.
javascript
// creating a map object
let myMap = new Map();
// Adding [key, value] pair to the map
myMap.set(0, 'geeksforgeeks');
myMap.set(1, 'is an online portal');
myMap.set(2, 'for geeks');
// displaying the number of
// [key, value] pairs the map has
console.log(myMap.size);
// removing the [key, value] pairs
// of the map using Map.clear() method
myMap.clear();
// displaying the number of
// [key, value] pairs the map has
console.log(myMap.size);
OUTPUT :
3
0
Applications:
Exceptions:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4