Set.prototype.clear()

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

Try it

Syntax

js
clear()

Parameters

None.

Return value

None (undefined).

Examples

Using the clear() method

js
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

Specification
ECMAScript Language Specification
# sec-set.prototype.clear

Browser compatibility

BCD tables only load in the browser

See also