CKTIS / JavaScript
Set.prototype.clear() — remove all elements
Set.prototype.clear() removes every element from an existing Set and returns undefined. It mutates the same object, so every reference to that object observes the emptied collection.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
set.clear()Call Set.prototype.clear() as set.clear(), without arguments. It removes every element; afterward, set.size is 0. Calling it on an empty set also returns undefined and leaves the set empty.
The method mutates the existing object rather than creating a new one. Other variables referencing that set also observe an empty collection. Assigning new Set() to one variable is not equivalent: other references still point to the previous object.
Unlike set.delete(value), which removes one specified element and returns a boolean, clear() removes all elements and returns undefined. Consequently, set.clear().add(value) cannot be used. Removing objects from the set does not modify those objects themselves.
The method requires a receiver with the internal [[SetData]] slot; Set.prototype.clear.call({}) throws TypeError. This is an ECMAScript standard-library feature, not a Web API. The language specification is not a browser compatibility table; check availability in the target environment.
Examples
const values = new Set([1, 2, 3]);
const alias = values;
const result = values.clear();
console.log(values.size);
console.log(alias === values);
console.log(alias.size);
console.log(result === undefined);Result
0 true 0 true
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.