JavaScript Map delete() Method

Last Updated : 15 Jun 2026

The JavaScript delete() method is used to remove a specific key-value pair from a Map object. It removes the entry associated with the specified key and indicates whether the deletion was successful.

In this chapter, you will learn how to use the delete() method to remove entries from a Map and understand its return value.

What is JavaScript Map delete() Method?

The JavaScript delete() method removes an element from a Map object based on the specified key. If the key exists, the entry is removed; otherwise, no changes are made to the Map.

Syntax of JavaScript Map delete() Method

Use the following syntax to remove an element from a Map object:

Parameters

The delete() method accepts a single parameter that specifies the key to remove.

  • key - The key of the element to be removed from the Map object.

Return Type/Value

The delete() method returns a Boolean value.

  • Returns true if the specified key exists and the corresponding entry is removed successfully.
  • Returns false if the specified key does not exist in the Map.

Note: Object keys are compared by reference, not by value.

Examples of JavaScript Map delete() Method

The following examples demonstrate different use cases of the delete() method.

Example 1: Remove a Key from a Map

In this example, we create a Map containing three entries and remove one of them using the delete() method. The size of the Map is displayed before and after the deletion.

Code

Execute Now

Output:

Size before invoking delete() method: 3
Size after invoking delete() method: 2

Explanation:

This JavaScript code creates a Map object named map, adds three key-value pairs to it utilizing the set() method where the keys are numbers (1, 2, and 3) and the values are strings ("jQuery", "AngularJS", and "Bootstrap") then it logs the size of the map utilizing the .size property which returns 3 at this point, deletes the entry with key 2 which removes the AngularJS pair utilizing the delete() method and finally logs the updated size of the map which becomes 2 which shows how entries can be dynamically removed from a Map and how it affects the total number of stored elements.

Example 2: Check Whether a Key Exists After Deletion

In this example, we use the has() method to check whether a specific key exists in the Map before and after removing it with the delete() method.

Code

Execute Now

Output:

Element present before invoking delete() method: true
Element present after invoking delete() method: false

Explanation:

This example creates a new Map() object and adds three key-value pairs where keys are numbers and values are strings which represents popular web technologies, it then checks if the key 2 exists in the Map if it exists then deletes the entry with key 2 and finally checks again to confirm that the entry was successfully removed which shows that the key 2 is no longer present.

Example 3: Delete a Non-Existent Key

In this example, we attempt to remove a key that does not exist in the Map. The return value of the delete() method shows whether the deletion was successful.

Code

Execute Now

Output:

false

Explanation:

In this example, we tried to delete a non-existent key. We created a variable named capitals utilizing the const keyword and assigned it a new Map() which creates a new Map object. We added two key-value pairs to the map where France is the key, Paris is the value and Spain is the key and Madrid is the value. Here, we defined another variable named wasDeleted and assigned it capitals.delete('Germany') to delete the Germany value. But Germany was never added so the key doesn't exist in the map. Therefore the delete() method returns false to indicate that no entry was removed.

Example 4: Delete an Object Key from a Map

In this example, objects are used as keys in a Map. The delete() method removes an entry using an object reference as the key and verifies that the entry no longer exists.

Code

Execute Now

Output:

true
false
1

Explanation:

In this example, we create two separate object literals, user1 and user2. Each object represents a user and is stored in memory by reference. We defined a variable named userSessions utilizing the const keyword and assigned it a new Map() which creates a new Map object. Here, we set two key-value pairs in the map. The user1 is the key for token123 and user2 is the key for token456. Here, the userSessions.delete(user1) is defined to delete the entry where the key is the object user1. The Map.prototype.delete() returns true if the key was found and removed. So deleted will be true. The second console checks if user1 still exists as a key in the map. It returns false because it was deleted.


Next TopicJavaScript Math