How to Remove from Set JavaScript

How to Remove from Set JavaScript | The set.delete() method in JavaScript deletes an element in a set with a supplied value and returns a boolean value based on the element’s availability. If the element with the supplied value does not exist in the set, it will yield false instead of modifying it. This article will give you an insight into how to remove an item from the set in JavaScript using different methods. Let us have a look at the following examples.

Example 1:
set = [cat, dog, pig, fish] 
Remove “fish” from the set
Set after removing the element:-
[cat, dog, pig]

Example 2:
set = [one, two, three, four, ten]
Remove “two” from the set
Set after removing the element:-
[one, three, four, ten]

The syntax for the delete() function is:- set1.delete(e)
The delete() method is used to remove/delete a particular piece e from a Set “set1”.

Let us just look at some illustrations of the delete() method in action to remove from the set in JavaScript:-

Approach to remove from set JavaScript:

1. Initialize a new string set using set() method and add new elements to it using the add() function. 
2. Print the set.
3. Use the myset.delete() function to remove an element from set in JavaScript.
4. The set without the deleted element will be printed.

// initialize a new set using Set()
let myset = new Set();

// add new elements to the set using add() 

// print the set

// Since 8993 exists, 
// it will be removed and it will return true

// Since 9999 doesn't exists, 
// Hence it will return false

// print the set


Set(2) { 8993, 123 }
Set(1) { 123 }

JavaScript Remove From Set using Function

Another way in which we can remove item from set in JavaScript is by creating our own function. We will define a function to remove a specific item from the given set.

var mySet = new Set(["PHP", "C++", "Python", "JavaScript"]);
var valuesRemove = new Set(["C++"]);

function removeAll(originalSet, toBeRemovedSet) {
   [...toBeRemovedSet].forEach(function (v) {

removeAll(mySet, valuesRemove);


[ ‘PHP’, ‘C++’, ‘Python’, ‘JavaScript’ ]
[ ‘PHP’, ‘Python’, ‘JavaScript’ ]

Method-3 to remove item from set JavaScript

Array.prototype.remByVal = function (val) {
   for (var i = 0; i < this.length; i++) {
      for (var j = 0; j < val.length; j++) {
         if (this[i] === val[j]) {
            this.splice(i, 1);
   return this;

var set = ["Mango", "Apple", "Orange", "Mark", "Banana", "Guava",];
var remove = ["Mark"];
set = set.remByVal(remove);


[ ‘Mango’, ‘Apple’, ‘Orange’, ‘Banana’, ‘Guava’ ]

This brings us to the end of this article, we hope you learned how to remove from set JavaScript. Also see:- Javascript Remove Last Digit from Number

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *