Horje
What is the use of the Map.prototype.clear method in JavaScript Maps ?

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

// Creating a map object
let myMap = new Map();
 
// Adding [key, value] pair to the map
myMap.set(0, 'horje');
 
// Displaying the number of
// [key, value] pairs the map has
console.log(myMap.size);
 
// Removing the [key, value] pairs of
// the map using Map.clear() method
myMap.clear();
 
// Displaying the number of
// [key, value] pairs the map has
console.log(myMap.size);

Output

1
0



Reffered: https://www.geeksforgeeks.org


JavaScript

Related
What is the use of the Get method in Maps in JavaScript ? What is the use of the Get method in Maps in JavaScript ?
Difference Between a Set and an Array Difference Between a Set and an Array
What is the use of the Add method in Sets in JavaScript ? What is the use of the Add method in Sets in JavaScript ?
How to Check if an Element Exists in a Set in JavaScript ? How to Check if an Element Exists in a Set in JavaScript ?
What Happens when you try to Add a Duplicate Value to a Set in JavaScript ? What Happens when you try to Add a Duplicate Value to a Set in JavaScript ?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
12