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.

New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources