In this section, we will learn what the delete() method is and how to use it in JavaScript.
What is JavaScript Map delete() Method?
We use the `delete()` method to delete a specific key from a map object.
Note: when a key is removed from a map, its value will be removed automatically as well.
JavaScript Map delete() Method Syntax:
delete(key);
delete() Function Parameters:
This method takes one argument and that is the key in which we want to remove.
delete() Function Return Value:
If the method removed the target key, it will return the value `true` which means the operation ran successfully. Otherwise it will return the value `false`.
One of the reasons that this method might return the value false is because the target key might not exist in the target map.
Example: using Map delete() method in JavaScript
const map = new Map(); map.set("John","222-22222222"); map.set(22 , 2000); map.set(true, false); map.set("Jack", "333-3234232"); if (map.delete('John')){ console.log(`The key 'John' successfully removed from the map.`); }
Output:
The key 'John' successfully removed from the map.