JavaScript .clear()

data2294409510's avatar
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.

  • 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

Syntax

map.clear()

Parameters:

  • It does not take any parameters.

Return value:

  • The method returns undefined after 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:

2
0
undefined

Codebyte Example

In this example, a fruit inventory map is initialized with values, then cleared to show that the map becomes empty:

Code
Output
Loading...

All contributors

Contribute to 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