What is the use of the Map.prototype.clear method in JavaScript Maps ?
Last Updated :
01 Feb, 2024
In JavaScript, the Map.prototype.clear
method is used to remove all key-value pairs from a Map, effectively making the Map empty. It does not take any arguments and does not return any value.
Syntax:
myMap.clear();
myMap
: The Map from which you want to remove all key-value pairs.
Example: Here, 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.
Javascript
let myMap = new Map();
myMap.set(0, 'geeksforgeeks' );
console.log(myMap.size);
myMap.clear();
console.log(myMap.size);
|
Share your thoughts in the comments
Please Login to comment...