Computer >> Computer tutorials >  >> Programming >> Javascript

Map.delete() function in JavaScript


The delete() function of Map object accepts a string representing the key of an element of a map and deletes from the current Map object. This function returns true if the specified key exists in the map object else it returns false.

Syntax

Its Syntax is as follows

mapVar.delete()

Example

<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.delete('4');
      document.write("Size of the map object: "+mapVar.size);
   </script>
</body>
</html>

Output

Size of the map object: 3

Example

<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.delete('4');
      document.write(map);
      document.write("<br>");
      document.write("Size of the map object: "+mapVar.size);
      document.write("<br>");
      var map = mapVar.delete('5');
      document.write(map);
      document.write("<br>");
      document.write("Size of the map object: "+mapVar.size);
   </script>
</body>
</html>

Output

true
Size of the map object: 3
false
Size of the map object: 3