What is the Map.delete method in JavaScript?

The delete method of the map object is used to remove an entry associated with a passed key.

Syntax

mapObj.delete(key);

Parameter

key: the key of the entry to be deleted from the map.

Return value

The delete method returns true if there is an entry for the passed key and returns false otherwise.

Code

let map = new Map();
map.set(1, "one");
map.set(2, "two");
console.log("The map is ", map);
console.log("Removing the key 1 : ", map.delete(1));
console.log("The map is ", map);
console.log("Removing the key 3 : ", map.delete(3));
console.log("The map is ", map);

Explanation

In the code above:

  • We created a map object with two entries.

  • We then called the delete method for the key 1. The key 1 is present. So the entry is removed from the map and true is returned.

  • We called the delete method for the key 3. The key 3 is not present so false is returned.

Free Resources