Set.prototype.clear
Removes all values from a Set.
Available since: ES2015
Syntax
set.clear()Return value
undefinedReturns nothing.
Examples
Clearing a set:
const selected = new Set([1, 2, 3]);
selected.clear();
console.log(selected.size);Output:
0Notes
If you need a new empty Set and the old one must stay as is, just create new Set().
See also
- Set.prototype.delete — Removes a value from a Set.
- Set.prototype.size — The number of values in a Set.
In the tutorial
- Map and Set — The Map and Set collections in JavaScript: a dictionary with keys of any type and a set of unique values.