Set.prototype.clear()

The clear() method removes all elements from a Set object.

Try it

Syntax

js

clear()

Return value

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("bar")); // false

Specifications

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

Browser compatibility

BCD tables only load in the browser

See also