set.delete(value) removes one value from a Set. It tells you what happened: it returns true if the value was in the Set and is now removed, and false if the value was not there.
It never throws an error for a missing value, so it is safe to call even when you are not sure the value exists. Use clear() when you want to remove everything.
Remember
set.delete(value)
value was in the Set → removed, returns true
value was not there → nothing changes, returns false
Objects must be the very same object
Never throws for a missing value
Syntax
Call it on a Set and pass the value to remove. Use the returned true or false if you need it.
Walk through the Set.for...of visits 1 to 6 in the order they were added.
Delete the even ones.n % 2 === 0 is true for 2, 4 and 6, so each of them is removed with delete(n) while the loop is running.
The loop is not confused. Removing the value that is being visited does not skip or repeat any other value. The Set ends with 1, 3, 5.
Compatibility
Browser & Runtime Support
Set.prototype.delete() is part of ES2015 (ES6), together with Set itself. Every current browser and Node.js has it.
✓ ES2015 · Widely supported
set.delete()
Supported in Chrome 38+, Edge 12+, Firefox 37+, Safari 9+, Opera 25+ and Node.js 4+. Internet Explorer 11 only has a basic Set.
ES2015Standard since
Google Chrome38+ · Desktop & Mobile
Full support
Microsoft Edge12+ · Chromium based
Full support
Mozilla Firefox37+ · Desktop & Mobile
Full support
Apple Safari9+ · macOS & iOS
Full support
Opera25+ · Chromium based
Full support
Samsung InternetRecent versions · Android
Supported
Android WebView38+ · Chromium based
Supported
Node.js4+ · Server runtime
Supported
DenoRecent versions · JavaScript runtime
Supported
BunRecent versions · JavaScript runtime
Supported
Internet ExplorerIE 11 · basic Set only
Partial support
set.delete()Excellent
Bottom line: Safe to use in every current browser and in Node.js. Only Internet Explorer needs special care.
Frequently Asked Questions
It returns true if the value was in the Set and has been removed, and false if the value was not there. It never throws an error because of a missing value.
Pass the very same object that is stored in the Set. A Set compares objects by identity, so a copy with the same properties is a different value and is not removed. Keep the object in a variable, or find it with a loop, before you delete it.
Yes. Deleting inside for...of or forEach() is safe. A value that you delete before the loop reaches it is skipped, and the other values are still visited once.
delete(value) removes one value and tells you with true or false. clear() removes every value and returns undefined. Use delete() for one item and clear() to start again from an empty Set.
No. delete() already returns false when the value is missing. Check has() only if you want to do something different before removing, because has() followed by delete() looks the value up twice.