Documentación offline JavaScript main

Set.prototype.clear()

main Documentación oficial Licencia CC-BY-SA-2.5Descargado el 2026-08-02

En esta página

The clear() method of {{jsxref("Set")}} instances removes all elements from this set.

{{InteractiveExample("JavaScript Demo: Set.prototype.clear()")}}

```js interactive-example const set = new Set(); set.add(1); set.add("foo");

console.log(set.size); // Expected output: 2

set.clear();

console.log(set.size); // Expected output: 0

## Syntax

```js-nolint
clear()

Parameters#

None.

Return value#

None ({{jsxref("undefined")}}).

Examples#

Using the clear() method#

const mySet = new Set();
mySet.add(1);
mySet.add("foo");

console.log(mySet.size); // 2
console.log(mySet.has("foo")); // true

mySet.clear();

console.log(mySet.size); // 0
console.log(mySet.has("foo")); // false

Specifications#

{{Specifications}}

Browser compatibility#

{{Compat}}

See also#

  • {{jsxref("Set")}}
  • {{jsxref("Set.prototype.delete()")}}