![]() |
VOOZH | about |
The delete() method in JavaScript Set deletes an element from the set and returns true if the element existed and was successfully deleted. Otherwise, it returns false if the element does not exist.
mySet.delete(value);Return value: It will return true if the value was present in the set and false if the value did not exist after removing the element.
Example 1: Removing Elements from a Set with the delete() Method
This code creates a new set called myset and adds two elements (75 and 12) using the add() method. Then, it prints the modified set. After that, it uses the delete() method to remove 75 from the set and prints the set again.
Set(2) { 75, 12 }
true
Set(1) { 12 }
Example 2: Deleting Non-Existent Elements from a Set with the delete() Method
The code initializes a Set, adds elements, attempts to delete a non-existent element (43), which returns false. The Set remains unchanged, containing [23, 12].
Set(2) { 23, 12 }
false
Set(2) { 23, 12 }