JavaScript Set Remove Element with delete()

Deepak Prasad
Short on time?Beta Get the gist of this 3-minute read in seconds, then ask follow-up questions

For related language topics, browse the JavaScript tutorial index. For more Python walkthroughs, open the Python tutorial index.

A JavaScript Set stores unique values. Populate it with JavaScript Set add(), then drop one entry with delete()—it removes the exact value 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.

Environment: Node.js v20.18.2. After each runnable snippet, the following paragraph states the expected console output (order and values).


Method 1: Remove an Element from a Set with delete()

Pass the value to delete().

javascript
const tags = new Set(["js", "css", "html"]);

console.log("set-delete:", tags.delete("css"));
console.log("set-values:", [...tags].join(","));
Output

You should see 2 lines, in order: set-delete: true, set-values: js,html.

The Set now contains "js" and "html".


Method 2: Check the Boolean Return Value

delete() returns false when the value is not in the Set.

javascript
const tags = new Set(["js", "html"]);

console.log("set-missing:", tags.delete("python"));
Output

You should see one line logging set-missing: false.

This 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. Set.has() tests collection membership, not plain-object keys—see check if key exists in object in JavaScript for when to use in or Object.hasOwn instead.

javascript
const users = new Set(["ana", "ravi"]);

if (users.has("ana")) {
  users.delete("ana");
}

console.log(users.has("ana"));
Output

You should see one line logging false.

You 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.

javascript
const user = { id: 1 };
const users = new Set([user]);

console.log(users.delete({ id: 1 }));
console.log(users.delete(user));
Output

You should see 2 lines, in order: false, true.

The 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.

javascript
const values = new Set([1, 2, 3]);
values.clear();

console.log(values.size);
Output

You should see one line logging 0.

Use 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. For construction, dedupe, and Map contrast, see HashSet in JavaScript (Set).


Official Documentation

Finished reading?Beta Get the key takeaways in seconds, then ask follow-up questions
Olorunfemi Akinlua

Boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs user-friendly interfaces to enhance digital experiences across various domains.

  • JavaScript
  • Web Design