What is the Map.clear method in JavaScript?

The clear method of the Map object is used to remove all the elements of the map object.

Syntax


mapObj.clear()

Return value

This method doesn’t return anything.

Code

let map = new Map();
map.set(1, "one");
map.set(2, "two");
console.log(map);
map.clear();
console.log("Map after calling map.clear");
console.log(map);

Explanation

In the code above, we created a map object and added two entries.


1 - one 
2 - two

Then, we called the clear method on the created map object. This will remove all the elements of the map.