Map.delete() In JavaScript

22 Nov, 2021
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 iterative data type and can be directly iterated whereas iterating an object requires obtaining its keys in a specific manner.
Map.delete() Method in JavaScriptThe Map.delete() method in JavaScript is used to delete the specified element among all the elements which are present in the map.
The Map.delete() method takes the key which needs to be removed from the map, thus removes the element associated with that key and returns true. If the key is not present then it returns false.
Applications:
Map.delete() is used to delete an element associated with the key among all the elements present in the map.
Syntax:
my_map.delete(key)

Parameters Used:
key: The element associated with this key is to be removed from the map
Return value:
The Map.delete() method returns true, if the key is present whose associated element is to removed which is passed as an argument, otherwise returns false.
Examples:
Input :  my_map.set(1, 'first');
         my_map.set(2, 'second');
         my_map.set(3,'third');
         my_map.set(4,'fourth');

         document.write(my_map.delete(3));

Output : true

Explanation: The key ‘3’ is present in the map hence element associated with it is removed and return true
Input :  my_map.set(1, 'first');
         my_map.set(2, 'second');
         my_map.set(3,'third');
         my_map.set(4,'fourth');
   
         document.write(my_map.delete(5));

Output : false

Explanation: The key ‘5’ is not present in the map hence it returns false.
Code 1:
<script>// creating a map objectvar my_map = new Map();  // Adding [key, value] pair to the mapmy_map.set(1, 'first');my_map.set(2, 'second');my_map.set(3,'third');my_map.set(4,'fourth');  // will display true as key '3'// is present and its associated  // element is removed as welldocument.write(my_map.delete(3),"</br>","</br>");    // elements left in the map after deletiondocument.write("key-value pair of the map",                " after deletion-","</br>");  my_map.forEach(function (item, key, mapObj) {     document.write(key.toString(),":",                   " ",item.toString() + "<br />"); });   </script>                        
Output:
true

key-value pair of the map after deletion-
1: first
2: second
4: fourth

Code 2:
<script>// creating a map objectvar my_map = new Map();  // Adding [key, value] pair to the mapmy_map.set(1, 'first');my_map.set(2, 'second');my_map.set(3,'third');my_map.set(4,'fourth');  // will display false as key '5'// is not present and its associated  // element is removed as welldocument.write(my_map.delete(5),               "</br>","</br>");  // elements left in the map after deletiondocument.write("key-value pair of the map",               " after deletion-","</br>");  my_map.forEach(function (item, key, mapObj) {     document.write(key.toString(),":"," ",                   item.toString() + "<br />"); });   </script>                    
Output:
false

key-value pair of the map after deletion-
1: first
2: second
3: third
4: fourth

If the key passed as an argument to the function is not present in the map then it returns false. Basically, it neither throws any exception nor does it have any error.
Difference in working of Map.clear(),Map.erase() and this functionMap.clear() removes all key-value pairs of the map and reduces the size of the map to zero. Whereas Map.erase() removes the specified mapped value whose key is passed as an argument or iterator or in range to remove pairs.
Supported Browser:
Chrome 38 and above
Edge 12 and above
Firefox 13 and above
Internet Explorer 11 and above
Opera 25 and above
safari 8 and above
ysachin2314
javascript-functions
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array
Roadmap to Learn JavaScript For Beginners
Difference Between PUT and PATCH Request
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?