MDN self-hosted library · e01.org

Set.prototype.clear()

The clear() method of Set instances removes all elements from this set.

no runnable cells javascript-instance-method upstream source

The clear() method of Set instances removes all elements from this set.

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 (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

Browser compatibility

See also

  • Set
  • Set.prototype.delete()