A JavaScript Set stores unique values. To remove one element from a Set, use the delete() method. It removes the exact value from the Set and returns true if the value existed or false if it was not found.
Use Set.delete() for one value, Set.clear() for all values, and Set.has() when you want to check membership before or after deletion.
Tested On: The examples were tested with Node.js v20.18.1 on a Linux system. The same Set behavior works in modern browsers and JavaScript runtimes.
Method 1: Remove an Element from a Set with delete()
Pass the value to delete().
const tags = new Set(["js", "css", "html"]);
console.log("set-delete:", tags.delete("css"));
console.log("set-values:", [...tags].join(","));Tested output:
set-delete: true
set-values: js,htmlThe Set now contains "js" and "html".
Method 2: Check the Boolean Return Value
delete() returns false when the value is not in the Set.
const tags = new Set(["js", "html"]);
console.log("set-missing:", tags.delete("python"));Tested output:
set-missing: falseThis return value is useful when you need to know whether anything changed.
Method 3: Check Before Removing with has()
Use has() when your code needs a separate existence check.
const users = new Set(["ana", "ravi"]);
if (users.has("ana")) {
users.delete("ana");
}
console.log(users.has("ana"));Output:
falseYou do not need has() if you only care about deleting the value; delete() already returns whether removal happened.
Method 4: Remove Objects from a Set
Objects are removed by reference, not by matching their contents.
const user = { id: 1 };
const users = new Set([user]);
console.log(users.delete({ id: 1 }));
console.log(users.delete(user));Output:
false
trueThe first delete fails because { id: 1 } creates a different object reference. The second delete succeeds because it uses the original object reference.
Method 5: Remove All Elements with clear()
Use clear() to empty the whole Set.
const values = new Set([1, 2, 3]);
values.clear();
console.log(values.size);Output:
0Use clear() when every value should be removed at once.
Common Questions About Removing from Set in JavaScript
How do I remove a value from a Set?
Use set.delete(value). It returns true if the value was removed and false if the value was not present.
How do I remove all values from a Set?
Use set.clear() to remove every value and reset set.size to 0.
Why does deleting an object from a Set fail?
A Set compares objects by reference. You must pass the same object instance that was added to the Set.
Summary
To remove an element from a Set in JavaScript, call delete() with the exact value. Check its boolean return value when you need to know whether the Set changed. Use clear() to remove all values, and remember that object values must be deleted with the same reference that was originally added.
