JavaScript – Delete an Element from Set

To delete an element from Set in JavaScript, call delete() method on this set and pass the specific element as argument to it.

delete() method returns a boolean value. The return value is true if the key is successfully removed from the set, else, false.

Syntax

The syntax to remove/delete a specific element e from a Set set1 using delete() method is

set1.delete(e)
ADVERTISEMENT

Examples

In the following example, we take a Set set1 with three elements. We shall delete the element 'banana' using delete() method.

index.html

If the specified element is not present, then delete() method does nothing and returns false.

In the following example, we try to delete an element 'cherry' which is not present in the Set set1.

index.html

Conclusion

In this JavaScript Tutorial, we learned how to delete a specific element from a Set in JavaScript using delete() method, with examples.