JavaScript .clear()
Published Oct 30, 2025
Contribute to Docs
The Map.clear() method in JavaScript removes all entries (key-value pairs) from a Map object. After invoking it, the map becomes empty and its .size property becomes 0.
Syntax
map.clear()
Parameters:
- It does not take any parameters.
Return value:
- The method returns
undefinedafter clearing the map.
Example
In this example, a map is created with two entries, then cleared, and the size and values are checked:
const myMap = new Map();myMap.set('a', 1);myMap.set('b', 2);console.log(myMap.size);myMap.clear();console.log(myMap.size);console.log(myMap.get('a'));
The output of this code will be:
20undefined
Codebyte Example
In this example, a fruit inventory map is initialized with values, then cleared to show that the map becomes empty:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn JavaScript on Codecademy
- Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
- Includes 34 Courses
- With Professional Certification
- Beginner Friendly.115 hours
- Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
- Beginner Friendly.15 hours