Set.prototype.delete
Removes the specified element from a Set object
Syntax
set.delete(value)Parameters
| Parameter | Type | Description |
|---|---|---|
| value | T | The value to remove |
Return Value
true if the value was in the set and has been removed, false otherwise
Examples
const set = new Set([1, 2, 3]);
console.log(set.delete(2)); // true
console.log(set.delete(4)); // false
console.log(set.size); // 2const online = new Set<string>();
online.add('alice');
online.add('bob');
online.delete('alice');
console.log([...online]); // ['bob']const set = new Set(['a', 'b', 'c', 'd']);
for (const val of set) {
if (val > 'b') set.delete(val);
}
console.log([...set]); // ['a', 'b']Understanding Set.prototype.delete
The Set.prototype.delete method in JavaScript removes the specified element from a Set object. It belongs to the Set object and is one of the most widely used methods for working with set values in modern JavaScript and TypeScript applications.
The method signature is set.delete(value). It accepts 1 parameter: value. When called, it returns true if the value was in the set and has been removed, false otherwise. Understanding when and how to use delete() helps you write more expressive, readable code.
Common use cases for Set.prototype.delete include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like set-add, set-has, set-clear, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for Set.prototype.delete is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
Set.prototype.addAppends a new element with a specified value to the end of a Set object
Set.prototype.hasReturns a boolean indicating whether an element with the specified value exists in a Set object or not
Set.prototype.clearRemoves all elements from a Set object
Map.prototype.deleteRemoves the specified element from a Map object by key
More Set Methods
Other methods in the Set object
Related Tools
More Set Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.