Map.clear() function in JavaScript


The clear() function of Map object removes all elements from the current Map object.

Syntax

Its Syntax is as follows

mapVar.clear()

Example

 Live Demo

<html>
<head>
   <title>JavaScript Example</title>
</head>
<body>
   <script type="text/javascript">
      var mapVar = new Map();
      mapVar.set('1', 'Java');
      mapVar.set('2', 'JavaFX');
      mapVar.set('3', 'HBase');
      mapVar.set('4', 'Neo4j');
      var map = mapVar.clear();
      document.write("Size of the map object: "+mapVar.size);
   </script>
</body>
</html>

Output

Size of the map object: 0

Example

 Live Demo

<html>
<head>
   <title>JavaScript Example</title>
</head>
<body>
   <script type="text/javascript">
      var mapVar = new Map();
      mapVar.set('1', 'Raju');
      mapVar.set('2', 'Ram');
      mapVar.set('3', 'Rahim');
      mapVar.set('4', 'Robert');
      document.write("Size of the map: "+mapVar.size);
      var map = mapVar.clear();
      document.write("<br>");
      document.write("Size of the map after clearing its contents using clear() function: "+mapVar.size);
   </script>
</body>
</html>

Output

Size of the map: 4
Size of the map after clearing its contents using clear() function: 0

Updated on: 25-Jun-2020

84 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements