JavaScript Map.delete() method

The Map.delete method in JavaScript is utilized to eliminate a particular element from a Map instance by referencing its key.

Syntax

The syntax for the Map.delete method is illustrated as follows:

Example

mapObj.delete(key);</p>

<h3 class="h3">Description:</h3>

<p><strong>key: </strong>The key of the element to remove from the Map object.</p>

<p><strong>Return Value: </strong>If an element in the Map object has been removed successfully then it returns true and if the key is not found in the map then it returns false.</p>

<h4 class="n">NOTE: Object keys are compared by reference, not by value.</h4>

<h2 class="h2">Examples of JavaScript Map.delete() method</h2>

<p>Here, we will understand the Map.delete() method through various examples.</p>

<h3 class="h3">Example 1: Example of utilizing the delete() method in JavaScript to remove a key from a map</h3>

<p>Let's see a simple example of how the delete() method in <a href="javascript-map">JavaScript map</a> removes a key.</p>

<div class="codewrapper"><h3 class="h3">Example</h3>

<div class="codeblock"><textarea class="xml" name="code">var map=new Map();  

map.set(1,"jQuery");  

map.set(2,"AngularJS");  

map.set(3,"Bootstrap");  

console.log("Size before invoking delete() method: "+ map.size);  

map.delete(2);  

console.log("Size after invoking delete() method: "+map.size);

Output:

Output

Size before invoking delete() method: 3

Size after invoking delete() method: 2

Explanation:

This JavaScript snippet initializes a Map object referred to as map and populates it with three key-value pairs by employing the set method. In this case, the keys are numerical values (1, 2, and 3), while the corresponding values are strings representing different frameworks ("jQuery", "AngularJS", and "Bootstrap"). Subsequently, it outputs the size of the map using the .size property, which, at this stage, indicates a value of 3. The code then proceeds to eliminate the entry associated with the key 2, thereby removing the pair linked to AngularJS by utilizing the delete method. Finally, it logs the modified size of the map, which decreases to 2, illustrating the dynamic nature of entries within a Map and the impact of such deletions on the overall count of stored elements.

Example 2: Example to determine whether the map object contains the specified element

Let’s examine an example to establish if a map object includes a particular element.

Example

Example

var map=new Map();  

map.set(1,"jQuery");  

map.set(2,"AngularJS");  

map.set(3,"Bootstrap");  

console.log("Element present before invoking delete() method: "+ map.has(2));  

map.delete(2);  

console.log("Element present after invoking delete() method: "+map.has(2));

Output:

Output

Element present before invoking delete() method: true

Element present after invoking delete() method: false

Explanation:

In this illustration, a new Map instance is instantiated and three key-value pairs are inserted, where the keys are numerical values and the corresponding values are strings that denote well-known web technologies. Subsequently, it verifies the presence of the key 2 within the Map; if the key is found, it removes the entry associated with key 2. Finally, a subsequent check is performed to ensure that the entry has been effectively eliminated, confirming that key 2 is no longer available in the Map.

Example 3: Example of trying to delete a non-existent Key

In this illustration, we will examine the result of attempting to remove a key that does not exist.

Example

Example

const capitals = new Map();

capitals.set('France', 'Paris');

capitals.set('Spain', 'Madrid');



const wasDeleted = capitals.delete('Germany'); // Key doesn't exist



console.log(wasDeleted); // false

Output:

Explanation:

In this illustration, we attempted to remove a key that does not exist. A variable referred to as capitals was established using the const keyword and was initialized with a new Map, thereby creating a fresh Map object. We inserted two key-value pairs into the map: with France as the key and Paris as the corresponding value, and with Spain as the key and Madrid as its value. Subsequently, we introduced another variable called wasDeleted, which we set to capitals.delete('Germany') in an effort to eliminate the value associated with Germany. However, since Germany was never included, the key is absent from the map. Consequently, the delete method yields false to signify that no entry was successfully removed.

Example 4: Example of deleting an object key

In this demonstration, we will explore the process of removing a key when that key is an object.

Example

Example

const userSessions = new Map();

const user1 = { name: 'Alice' };

const user2 = { name: 'Bob' };

userSessions.set(user1, 'token123');

userSessions.set(user2, 'token456');



const deleted = userSessions.delete(user1);

console.log(deleted);           // true

console.log(userSessions.has(user1)); // false

console.log(userSessions.size);       // 1

Output:

Output

true

false

1

Explanation:

In this illustration, we instantiate two distinct object literals, referred to as user1 and user2. Each of these objects symbolizes a user and is referenced in memory. We declare a variable called userSessions using the const keyword and initialize it with a new Map, which constructs a new Map object. Within this map, we establish two key-value pairs: user1 corresponds to token123 and user2 corresponds to token456. Subsequently, we invoke userSessions.delete(user1) to remove the entry associated with the key user1. The Map.prototype.delete method yields true if the specified key was located and successfully removed. Therefore, deleted will evaluate to true. The subsequent console log examines whether user1 remains as a key within the map, yielding false since it has been deleted.

Conclusion:

In JavaScript, the Map.delete function is employed to eliminate a particular key-value association from a map using its corresponding key. The method returns true if the key was present and has been removed successfully. This topic is essential in JavaScript programming. Regardless of whether you are a novice or an advanced developer, this article aims to provide you with a comprehensive understanding of the delete method.

Frequently Asked Questions (FAQs):

  1. What is the purpose of using Map.delete(key)?

The Map.delete(key) function is employed to eliminate the entry associated with the specified key from the Map, provided that it is present. If the key is found, the method returns true and removes the corresponding key-value pair, thereby reducing the map's size. Conversely, if the key is not located, it returns false and the map remains unaltered.

  1. Does delete throw errors on invalid inputs?

No, it does not generate an error for invalid inputs. When we provide undefined, null, or a key that does not exist, it returns false. There is no error thrown in cases of an invalid or absent key.

  1. What occurs if we remove a key that was added recently?

When we eliminate a key that has been added recently, it will effectively be removed from the collection.

  1. Is it possible for the delete function to remove keys that are computed or dynamic in nature?

Indeed, when we assign a dynamic key to a variable and subsequently use that variable in the delete method, it will effectively eliminate computed or dynamic keys.

  1. What is the return value of Map.delete?

It returns a boolean value of true if the specified key was present and has been successfully deleted; otherwise, it returns false. No error is thrown if the key is absent or undefined. This return value is useful for determining the success of the deletion operation. The map is included in the official ECMAScript specification for the Map data structure.

Input Required

This code uses input(). Please provide values below: