The clear
method of the Map
object is used to remove all the elements of the map object.
mapObj.clear()
This method doesn’t return anything.
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);
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.