Map.clear( ) In JavaScript - GeeksforGeeks

21 Jan, 2022
What is a map in JavaScript?
Map is a data structure in JavaScript which allows storing of [key, value] pairs where any value can be either used as a key or value.
The keys and values in the map collection may be of any type and if a value is added to the map collection using a key which already exists in the collection, then the new value replaces the old value.
The iteration of elements in a map object is done in the insertion order and a “for...” loop returns an array of all [key, value] pairs for each iteration.
Differences between Objects and Maps in JavaScriptBoth of these data structures are similar in many ways such as both store values using keys, allow retrieval of those values using keys, deletion of keys and verify whether a key holds any value or not. However, there are quite significant differences between Objects and Maps in JavaScript which make the usage of maps a better and more preferable option in many cases.
The keys used in maps can be any type of values such as functions, objects etc whereas the keys in objects are limited to symbols and strings.
The size of a map can be known easily by using the size property but while dealing with objects, the size has to be determined manually.
A Map should be preferred in cases where the requirement involves frequent addition and removal of [key, value] pairs because a map is an iterable data type and can be directly iterated whereas iterating an object requires obtaining its keys in a specific manner.
Map.clear() Method in JavaScriptThe Map.clear() method in JavaScript is used for the removal of all the elements from a map and make it empty. It removes all the [key, value] from the map. No arguments are required to be sent as parameters to the Map.clear() method and it returns an undefined return value.
Syntax:
mapObj.clear()
Parameters Used:No parameters are required in the Map.clear() method.
Return Value:Map.clear() method has an undefined return type.
Examples of the above function are provided below.
Examples:
Input : var myMap = new Map();
        myMap.set(0, 'geeksforgeeks');
        console.log(myMap.size);
        myMap.clear();
        console.log(myMap.size);

Output: 1
        0

Explanation: In this example, a map object “myMap” has been created with a single [key, value] pair and the Map.clear() method is used to remove the [key, value] pair from “myMap”. myMap.size() is used to check the number of [key, value] pairs belonging to the map object.
Input : var myMap = new Map();
        myMap.set(0, 'geeksforgeeks');
        myMap.set(1, 'is an online portal');
        myMap.set(2, 'for geeks');
        console.log(myMap.size);
        myMap.clear();
        console.log(myMap.size);
Output : 3
         0

Explanation: In this example, a map object “myMap” has been created with a three [key, value] pairs and the Map.clear() method is used to remove all the [key, value] pairs from “myMap”. myMap.size() is used to check the number of [key, value] pairs belonging to the map object.
Codes for the above function are provided below.
Code 1:
<script>      // creating a map object    var myMap = new Map();  // Adding [key, value] pair to the mapmyMap.set(0, 'geeksforgeeks');  // displaying the number of // [key, value] pairs the map hasdocument.write(myMap.size);document.write("<br>");  // removing the [key, value] pairs of// the map using Map.clear() methodmyMap.clear();  // displaying the number of // [key, value] pairs the map hasdocument.write(myMap.size);  </script>                    s
OUTPUT :
1
0

Code 2:
<script>    // creating a map object    var myMap = new Map();  // Adding [key, value] pair to the mapmyMap.set(0, 'geeksforgeeks');myMap.set(1, 'is an online portal');myMap.set(2, 'for geeks');  // displaying the number of // [key, value] pairs the map hasdocument.write(myMap.size);document.write("<br>");  // removing the [key, value] pairs// of the map using Map.clear() methodmyMap.clear();  // displaying the number of// [key, value] pairs the map hasdocument.write(myMap.size);  < /script>                    
OUTPUT :
3
0

Applications:
Map.clear() Method is used to remove all the [key, value] pairs of a map.Let’s see a JavaScript Program :<script>      // creating a map object    var myMap = new Map();  // Adding [key, value] pair to the mapmyMap.set(0, 'Maps');myMap.set(1, 'in JavaScript');  // displaying the number of// [key, value] pairs the map hasdocument.write(myMap.size);document.write("<br>");  // removing the [key, value] pairs of// the map using Map.clear() methodmyMap.clear();  // displaying the number of // [key, value] pairs the map hasdocument.write(myMap.size);  <script>                    OUTPUT :     2
     0

Let’s see a JavaScript Program :
<script>      // creating a map object    var myMap = new Map();  // Adding [key, value] pair to the mapmyMap.set(0, 'Maps');myMap.set(1, 'in JavaScript');  // displaying the number of// [key, value] pairs the map hasdocument.write(myMap.size);document.write("<br>");  // removing the [key, value] pairs of// the map using Map.clear() methodmyMap.clear();  // displaying the number of // [key, value] pairs the map hasdocument.write(myMap.size);  <script>                    
OUTPUT :
     2
     0

Exceptions :
If the variable is not of the Map type then the Map.entries() operation throws a TypeError.
Supported Browsers:
Chrome 38 and above
Edge 12 and above
Firefox 19 and above
Internet Explorer 11 and above
Opera 25 and above
Safari 8 and above
Reference :https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/clear
ysachin2314
javascript-functions
javascript-map
JavaScript
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to append HTML code to a div using JavaScript ?
How to Open URL in New Tab using JavaScript ?
Difference Between PUT and PATCH Request
JavaScript | console.log() with Examples
Set the value of an input field in JavaScript
Node.js | fs.writeFileSync() Method
How to read a local text file using JavaScript?